Data is the lifeblood of modern businesses. In today’s data-driven world, companies rely on data to make informed decisions, gain a competitive edge, and provide exceptional customer experiences. However, not all data is created equal. Poor-quality data can lead to incorrect insights, bad decisions, and lost opportunities.
AWS Glue Data Quality measures and monitors the quality of your dataset. It supports both data quality at rest and data quality in AWS Glue extract, transform, and load (ETL) pipelines. Data quality at rest focuses on validating the data stored in data lakes, databases, or data warehouses. It ensures that the data meets specific quality standards before it is consumed. Data quality in ETL pipelines, on the other hand, ensures the quality of data as it moves through the ETL process. It helps identify data quality issues during the ETL pipeline, allowing for early detection and correction of problems and prevents the failure of the data pipeline because of data quality issues.
This is Part 3 of a five-post series on AWS Glue Data Quality. In this post, we demonstrate the advanced data quality checks that you can typically perform when bringing data from a database to an Amazon Simple Storage Service (Amazon S3) data lake. Check out the other posts in this series:
Let’s consider an example use case where we have a database named classicmodels that contains retail data for a car dealership. This example database includes sample data for various entities, such as Customers, Products, ProductLines, Orders, OrderDetails, Payments, Employees, and Offices. You can find more details about this example database in MySQL Sample Database.
In this scenario, we assume the role of a data engineer who is responsible for building a data pipeline. The primary objective is to extract data from a relational database, specifically an Amazon RDS for MySQL database, and store it in Amazon S3, which serves as a data lake. After the data is loaded into the data lake, the data engineer is also responsible for performing data quality checks to ensure that the data in the data lake maintains its quality. To achieve this, the data engineer uses the newly launched AWS Glue Data Quality evaluation feature.
The following diagram illustrates the entity relationship model that describes the relationships between different tables. In this post, we use the employees, customers, and products table.

This solution focuses on transferring data from an RDS for MySQL database to Amazon S3 and performing data quality checks using the AWS Glue ETL pipeline and AWS Glue Data Catalog. The workflow involves the following steps:
The following diagram illustrates the solution architecture.

To implement the solution, we complete the following steps:
This post includes a CloudFormation template for a quick setup. You can review and customize it to suit your needs. The template generates the following resources:
To launch the CloudFormation stack, complete the following steps:
This stack can take around 10 minutes to complete, after which you can view the deployed stack on the AWS CloudFormation console.

To connect to the RDS for MySQL instance, complete the following steps:


To run your crawler, complete the following steps:
dq-rds-crawler.The crawler will take a few minutes to crawl all the tables from the classicmodels database.

To validate the Data Catalog when the crawler is complete, complete the following steps:
mysql\_private\_classicmodels database.You will able to see all the RDS tables available under mysql\_private\_classicmodels.

To run your ETL job, complete the following steps:
dq-rds-to-s3 from the job list and choose Run job.
When the job is complete, you will able to see three new tables under mysql\_s3\_db. It may take a few minutes to complete.

Now let’s dive into evaluating the data quality rules.
In this section, we evaluate the results of different data quality rules.
Let’s start with referential integrity. The ReferentialIntegrity data quality ruleset is currently supported in ETL jobs. This feature ensures that the relationships between tables in a database are maintained. It checks if the foreign key relationships between tables are valid and consistent, helping to identify any referential integrity violations.
ReferentialIntegrity.mysql\_private\_classicmodels.mysql\_classicmodels\_employees.
customers.mysql\_private\_classicmodels.mysql\_classicmodels\_employees.employees and customers.All other datasets are used as references to ensure that the primary dataset has good-quality data.

ReferentialIntegrity under Rule types and choose the plus sign to add an example ReferentialIntegrity rule.
GlueServiceRole-for-gluedq-blog for IAM role and keep the remaining options as default.It will take a few minutes to complete.
You can confirm if the job completed successfully and which data quality rules it passed. In this example, it indicates that 60–70% of EmployeeNumber from the employees table are present in the customers table.

You can identify which records failed the referential integrity using AWS Glue Studio. To learn more, refer to Getting started with AWS Glue Data Quality for ETL Pipelines.
Similarly, if you are checking if all the EmployeeNumber from the employees table are present in the customers table, you can pass the following rule:
DatasetMatch compares two datasets to identify differences and similarities. You can use it to detect changes between datasets or to find duplicates, missing values, or inconsistencies across datasets.
RDS DatasetMatch.rds\_employees\_primary.mysql\_private\_classicmodels.mysql\_classicmodels\_employees.s3\_employees\_reference.mysql\_s3\_db.s3\_employees.employees and customers.rds\_employees\_primary and for s3\_employees\_reference, enter the alias reference.
GlueServiceRole-for-gluedq-blog for IAM role and keep the remaining options as default.In this example, it indicates both datasets are identical.

AggregateMatch verifies the accuracy of aggregated data. It compares the aggregated values in a dataset against the expected results to identify any discrepancies, such as incorrect sums, averages, counts, or other aggregate calculations. This is a performant option to evaluate if two datasets match at an aggregate level. For this rule, we clone the previous job we created for DatasetMatch.
RDS DatasetMatch and on the Actions menu, choose Clone job.
DQ AggregateMatch.rds\_employees\_primary to rds\_products\_primary and the table to mysql\_classicmodels\_products.s3\_orders\_reference to s3\_products\_reference and the table to s3\_products.rds\_products\_primary and s3\_products\_reference.
The results indicate that the avg(msrp) on both datasets is the same.

RowCountMatch checks the number of rows in a dataset and compares it to an expected count. It helps identify missing or extra rows in a dataset, ensuring data completeness. For this rule, we edit the job we created earlier for AggregateMatch.
AggregateMatch and on the Actions menu, choose Edit job.
RowCountMatch.
RowCountMatch "reference" = 1.0 
It shows that the DQ RowCountMatch rule failed, indicating a mismatch between the row count of the source RDS table and the target S3 table. Further investigation reveals that the ETL job ran four times for the Products table, and the row counts didn’t match.

SchemaMatch validates the schema of two datasets matches. It checks if the actual data types match the expected data types and flags any inconsistencies, such as a numeric column containing non-numeric values. For this rule, we edit the job we used for AggregateMatch.
RDS AggregateMatch and on the Actions menu, choose Edit job.
RowCountMatch.
It should show a successful completion with a Rule passed status, indicating that the schemas of both datasets are identical.

The AWS Glue Data Catalog also supports advanced data quality rules. For this post, we show one example of an aggregate match between Amazon S3 and Amazon RDS.
mysql\_private\_classicmodels database to view the three tables created under it.mysql\_classicmodels\_products table.
AggregateMatch and choose the plus sign to view the default example rule.reference is the alias of the secondary dataset defined in the AWS Glue ETL job. For the Data Catalog, you can use <database\_name>.<table\_name>.<column\_name> to reference secondary datasets.
production\_catalog\_dq\_check.GlueServiceRole-for-gluedq-blog for IAM role and keep the remaining options as default.When the job is complete, you can confirm that both data quality checks passed.

With these advanced data quality features of AWS Glue Data Quality, you can enhance the reliability, accuracy, and consistency of your data, leading to better insights and decision-making.
To clean up your resources, complete the following steps:
Data quality refers to the accuracy, completeness, consistency, timeliness, and validity of the information being collected, processed, and analyzed. High-quality data is essential for businesses to make informed decisions, gain valuable insights, and maintain their competitive advantage. As data complexity increases, advanced rules are critical to handle complex data quality challenges. The rules we demonstrated in this post can help you manage the quality of data that lives in disparate data sources, providing you the capabilities to reconcile them. Try them out and provide your feedback on what other use cases you need to solve!
Navnit Shukla is AWS Specialist Solutions Architect in Analytics. He is passionate about helping customers uncover insights from their data. He builds solutions to help organizations make data-driven decisions.
Rahul Sharma is a Software Development Engineer at AWS Glue. He focuses on building distributed systems to support features in AWS Glue. He has a passion for helping customers build data management solutions on the AWS Cloud.
Edward Cho is a Software Development Engineer at AWS Glue. He has contributed to the AWS Glue Data Quality feature as well as the underlying open-source project Deequ.
Shriya Vanvari is a Software Developer Engineer in AWS Glue. She is passionate about learning how to build efficient and scalable systems to provide better experience for customers. Outside of work, she enjoys reading and chasing sunsets.