Getting started with Amazon EMR Data Quality
This section walks you through running your first Amazon EMR Data Quality evaluation end to end: the permissions your Amazon EMR Spark application needs, and a walkthrough that submits a DQDL ruleset against a DataFrame on Amazon EMR and retrieves the result.
Prerequisites
IAM permissions
The IAM role that your Amazon EMR Spark workload assumes — the EC2 instance profile attached to the cluster, or a per-step runtime role — must grant the following permissions to run Data Quality and publish results.
| Action | When required |
|---|---|
glue:PublishDataQuality |
To publish each evaluation to Amazon Glue Data Quality so you can
view history, compare runs, and feed anomaly detection.
Publishing is on by default
(enableDataQualityResultsPublishing is
True), so grant this permission unless you
explicitly opt out by setting the option to
False. |
glue:AccessDataQualityRuntimeConfiguration |
To fetch publishing configuration from the service when the library initializes publishing. Required whenever publishing is enabled; without it, the library silently disables publishing for the run. |
cloudwatch:PutMetricData |
To emit per-run pass and fail metrics to Amazon CloudWatch
for dashboards and alarms. Required only when you set
enableDataQualityCloudWatchMetrics to
True. |
glue:GetTable,
glue:GetDatabase |
To read catalog table metadata. Required when the
input DataFrame is read from a Amazon Glue Data Catalog table, or
when the application passes
publishToDataCatalogTarget so the service
can attribute the run to a specific catalog
table. |
glue:GetDataQualityRuleset |
To fetch a saved ruleset from the Amazon Glue Data Catalog. Required only when your Spark script loads a ruleset from the catalog instead of defining DQDL inline. See Fetching a saved ruleset from the Amazon Glue Data Catalog. |
The following inline policy grants the minimum set of permissions required to publish Data Quality results from an Amazon EMR Spark application.
{ "Version": "2012-10-17", "Statement": [ { "Sid": "GlueDataQuality", "Effect": "Allow", "Action": [ "glue:PublishDataQuality", "glue:AccessDataQualityRuntimeConfiguration" ], "Resource": "*" } ] }
Run your first evaluation
-
Create an Amazon EMR cluster or application. Use release
emr-spark-8.1.0or later on Amazon EMR on EC2, Amazon EMR on EKS, or Amazon EMR Serverless. Attach a role that has the permissions listed in IAM permissions.For example, to create an Amazon EMR on EC2 cluster, run:
aws emr create-cluster --release-label emr-spark-8.1.0 \ --applications Name=Spark \ --region us-east-1 \ --name My_Spark_DataQuality_Cluster \ --log-uri s3://amzn-s3-demo-bucket/ \ --instance-type m5.xlarge \ --instance-count 2 \ --service-role EMR_DefaultRole_V2 \ --ec2-attributes InstanceProfile=EMR_EC2_DefaultRole,SubnetId=subnet-1234567890abcdef0 -
Define a ruleset and evaluate it. The following PySpark script defines a ruleset in DQDL, evaluates it against a DataFrame, and prints the per-rule outcome.
from pyspark.sql import SparkSession from awsdataquality.transforms import EvaluateDataQuality spark = SparkSession.builder.appName("dq-example").getOrCreate() df = spark.read.parquet("s3://amzn-s3-demo-bucket/orders/") ruleset = """ Rules = [ RowCount > 1000, IsComplete "order_id", IsUnique "order_id", ColumnValues "status" in ["PLACED", "SHIPPED", "CANCELED"], Mean "order_total" between 10 and 500 ] """ results = EvaluateDataQuality.apply( frame=df, ruleset=ruleset, publishing_options={ "dataQualityEvaluationContext": "orders_daily", "enableDataQualityResultsPublishing": True, }, ) results.show(truncate=False)The
resultsDataFrame contains one row per rule with the following columns:Rule,Outcome,FailureReason, andEvaluatedMetrics. Rows with anOutcomeofFailedinclude the observed value inFailureReason. -
Submit the application. Submit the script the same way you submit any other Spark application for your Amazon EMR deployment mode.
-
Retrieve the result. After the application finishes, list the run and fetch its detail with the Amazon Glue Data Quality APIs or the Amazon CLI. If your application also sets
publishToDataCatalogTargetwith aDatabaseNameandTableName, the run appears on the Data quality tab of that catalog table in the Amazon Glue console (see Viewing results in the Amazon Glue console). Otherwise, use the API or Amazon CLI to retrieve the run.aws glue list-data-quality-results-v2 --max-results 5 aws glue get-data-quality-result-v2 --result-id<result-id>To narrow the list to runs of a specific dataset, pass the
EvaluationContextthat your application used (for example,orders_daily) toListDataQualityStatistics. When your application reads from a Amazon Glue Data Catalog table, you can additionally filterlist-data-quality-results-v2byDataSource.GlueTable.
Fetching a saved ruleset from the Amazon Glue Data Catalog
Instead of defining your ruleset inline in each Spark script, you can author a ruleset once in the Amazon Glue Data Catalog and fetch it from your Amazon EMR Spark application at runtime. This lets you version a ruleset in one place and share it across Amazon EMR Spark workloads, Amazon Glue ETL jobs, and evaluations.
-
Create the ruleset in Amazon Glue. Author and save the ruleset using one of the following paths:
-
In the Amazon Glue console. See Creating a new ruleset in the Amazon Glue Developer Guide.
-
Through the Amazon Glue Data Quality API. See
CreateDataQualityRulesetin the Amazon Glue API Reference.
-
-
Grant the Spark workload role access. Add
glue:GetDataQualityRulesetto the role that your Amazon EMR Spark application assumes. -
Fetch the ruleset in your Spark script. Call
GetDataQualityRulesetwith boto3, extract theRulesetfield, and pass the DQDL string toEvaluateDataQuality.apply.import boto3 from awsdataquality.transforms import EvaluateDataQuality glue = boto3.client("glue") response = glue.get_data_quality_ruleset(Name="orders_quality") ruleset = response["Ruleset"] results = EvaluateDataQuality.apply( frame=df, ruleset=ruleset, publishing_options={ "dataQualityEvaluationContext": "orders_daily", "enableDataQualityResultsPublishing": True, }, )