Thursday, February 9, 2023
HomeBig DataUse SQL queries to outline Amazon Redshift datasets in AWS Glue DataBrew

Use SQL queries to outline Amazon Redshift datasets in AWS Glue DataBrew

Within the put up Knowledge preparation utilizing Amazon Redshift with AWS Glue DataBrew, we noticed learn how to create an AWS Glue DataBrew job utilizing a JDBC connection for Amazon Redshift. On this put up, we present you learn how to create a DataBrew profile job and a recipe job utilizing an Amazon Redshift reference to customized SQL.

DataBrew is a visible knowledge preparation device that may aid you simplify your extract, remodel, and cargo (ETL) course of. Now you can outline a dataset from Amazon Redshift by making use of customized SQL statements. Making use of a customized SQL assertion to a big supply desk permits you to choose, be a part of, and filter the info earlier than cleansing, normalizing, and remodeling it in a DataBrew venture. Filtering and becoming a member of the info out of your knowledge supply and solely bringing within the knowledge you wish to remodel simplifies the ETL course of.

On this put up, we display learn how to use customized SQL queries to outline your Amazon Redshift datasets in DataBrew.

Resolution overview

To implement this answer, you full the next high-level steps:

  1. Create an Amazon Redshift connection.
  2. Create your dataset and use SQL queries to outline your Amazon Redshift supply datasets.
  3. Create a DataBrew profile job to profile the supply knowledge.
  4. Create a DataBrew venture and recipe job to remodel the info and cargo it to Amazon Easy Storage Service (Amazon S3).

The next diagram illustrates the structure for our answer.


To make use of this answer, full the next prerequisite steps:

  1. Have an AWS account.
  2. Create an Amazon Redshift cluster in a non-public subnet inside a VPC as a safety finest apply.
  3. As a result of DataBrew instructions require that the cluster has entry to Amazon S3, ensure you create a gateway VPC endpoint to Amazon S3. The gateway endpoint gives dependable connectivity to Amazon S3 with out requiring an web gateway or NAT gadget out of your VPC.
  4. Allow the enhanced VPC routing within the Amazon Redshift cluster. Enhanced VPC routing forces all Amazon Redshift instructions to make use of the connectivity to the gateway VPC endpoint to Amazon S3 in the identical AWS Area as your cluster.
  5. Create a database and tables, and cargo the pattern knowledge within the Amazon Redshift cluster.
  6. Put together a SQL question to extract the supply dataset. You employ this SQL question later on this put up to create an Amazon Redshift supply dataset in DataBrew.
  7. Create an S3 bucket to retailer knowledge from the profile and recipe jobs. The DataBrew connection quickly shops intermediate knowledge in Amazon S3.
  8. For our use case, we use a mock dataset. You possibly can obtain the DDL and knowledge information from GitHub.

Safety finest practices

Contemplate the next finest practices with the intention to mitigate safety threats:

  • Overview the shared accountability mannequin when utilizing DataBrew.
  • Limit community entry for inbound and outbound site visitors to least privilege. Benefit from the routing site visitors inside the VPC through the use of an Amazon S3 gateway endpoint and enhanced VPC routing in Amazon Redshift.
  • Allow the lifecycle coverage in Amazon S3 to retain solely vital knowledge, and delete pointless knowledge.
  • Allow Amazon S3 versioning and cross-Area replication for crucial datasets to guard towards unintended deletes.
  • Allow server-side encryption utilizing AWS KMS (SSE-KMS) or Amazon S3 (SSE-S3).
  • DataBrew makes use of Amazon CloudWatch for logging, so you need to replace your log retention interval to retain logs for the suitable size of time.

Create an Amazon Redshift connection

On this part, you create a connection in DataBrew to hook up with your Amazon Redshift cluster.

  1. On the DataBrew console, select Datasets within the navigation pane.
  2. On the Connections tab, select Create connection.
  3. For Connection identify, enter a reputation, reminiscent of order-db-connection.
  4. For Connection sort, choose Amazon Redshift.
  5. Underneath Connection entry, present the Amazon Redshift cluster identify, database identify, database consumer, and database password.
  6. Select Create connection.

Create your dataset by making use of a customized SQL assertion to filter the supply knowledge

On this part, you create a Amazon Redshift connection, add your customized SQL assertion, and validate it. You may as well validate your SQL assertion immediately in your Amazon Redshift cluster through the use of the Amazon Redshift question editor v2. The aim of validating the SQL assertion is that will help you keep away from failure in loading your dataset right into a venture or job. Additionally, checking the question runtime ensures that it runs in below 3 minutes, avoiding timeouts throughout venture loading. To research and enhance question efficiency in Amazon Redshift, see Tuning question efficiency.

  1. On the DataBrew console, select Datasets within the navigation pane.
  2. On the Datasets tab, select Join new dataset.
  3. For Dataset identify, enter a reputation, reminiscent of order-data.
  4. Within the left pane, select Amazon Redshift below Database connections.
  5. Add your Amazon Redshift connection and choose Enter customized SQL.
  6. Enter the SQL question and select Validate SQL.
  7. Underneath Extra configurations, for Enter S3 vacation spot, present an S3 vacation spot to quickly retailer the intermediate outcomes.
  8. Select Create dataset.

Create a DataBrew profile job

On this part, you employ the newly created Amazon Redshift dataset to create a profile job. Knowledge profiling helps you perceive your dataset and plan the info preparation steps wanted in working your recipe jobs.

  1. On the DataBrew console, select Jobs within the navigation pane.
  2. On the Profile jobs tab, select Create job.
  3. For Job identify, enter a reputation, reminiscent of order-data-profile-job.
  4. For Job sort¸ choose Create a profile job.
  5. Underneath Job enter, select Browse datasets and select the dataset you created earlier (order-data).
  6. For Knowledge pattern, choose Full dataset.
  7. Underneath Job output settings¸ for S3 location, enter the S3 bucket for the job output information.
  8. For Position identify, select an AWS Id and Entry Administration (IAM) position with permission for DataBrew to hook up with the info in your behalf. For extra info, check with Including an IAM position with knowledge useful resource permissions.
  9. Select Create and run job.

Examine the standing of your profile job. A profile output file is created and saved in Amazon S3 upon completion. You possibly can select View knowledge profile to see extra info.

Along with an output file, DataBrew additionally gives visualizations. On the Dataset profile overview tab, you possibly can see knowledge visualizations that may aid you perceive your knowledge higher. Subsequent, you possibly can see detailed statistics about your knowledge on the Column statistics tab, illustrated with graphics and charts. You possibly can outline knowledge high quality guidelines on the Knowledge high quality guidelines tab, after which see the outcomes from the info high quality ruleset that applies to this dataset.

For instance, within the following screenshot, the quantity column has 2% lacking values, as proven on the Column statistics tab. You possibly can present guidelines that keep away from triggering a recipe job in case of an anomaly. You may as well notify the supply groups to deal with or acknowledge the lacking values. DataBrew customers may also add steps within the recipe job to deal with the anomalies and lacking values.

Create a DataBrew venture and recipe job

On this part, you begin analyzing and remodeling your Amazon Redshift dataset in a DataBrew venture. The customized SQL assertion runs in Amazon Redshift when the venture is loaded. Databrew performs read-only entry to your supply knowledge.

Create a venture

To create your venture, full the next steps:

  1. On the DataBrew console, select Initiatives within the navigation pane.
  2. Select Create venture.
  3. For Undertaking identify, enter a reputation, reminiscent of order-data-proj.
  4. Underneath Recipe particulars¸ select Create new recipe and enter a recipe identify, reminiscent of order-data-proj-recipe.
  5. For Choose a dataset, choose My datasets.
  6. Choose the dataset you created earlier (order-data).
  7. Underneath Permissions, for Position identify, select your DataBrew position.
  8. Select Create venture.

DataBrew begins a session, constructs a DataFrame, extracts pattern knowledge, infers primary statistics, and shows the pattern knowledge in a grid view. You possibly can add steps to construct a metamorphosis recipe. As of this writing, DataBrew presents over 350 transformations, with extra on the way in which.

For our instance use case, Firm ABC has set a goal to ship all orders inside 7 days after the order date (inner SLA). They need a listing of orders that didn’t meet the 7-day SLA for added investigation. The next pattern recipe comprises steps to deal with the lacking values, filter the values by quantity, change the date format, calculate the date distinction, and filter the values by delivery days. The detailed steps are as follows:

  1. Fill lacking values with 0 for the quantity column.
  2. Filter values by quantity higher than 0.
  3. Change the format of order_timestamp to align with ship_date.
  4. Create a brand new column known as days_for_shipping utilizing the dateTime perform DATEDIFF to indicate the distinction between order_timestamp and ship_date in days.
  5. Filter the values by days_for_shipping higher than 7.

Create a recipe job

To create your DataBrew recipe job, full the next steps:

  1. On the DataBrew console, select Jobs within the navigation pane.
  2. Select Create job.
  3. For Job identify¸ enter a reputation, reminiscent of SHIPPING-SLA-MISS.
  4. Underneath Job output settings, configure your Amazon S3 output settings.
  5. For S3 location, enter the placement of your output bucket.
  6. For Position identify, select the IAM position that comprises permissions for DataBrew to attach in your behalf.
  7. Select Create and run job.

You possibly can test the standing of your job on the Jobs web page.

The output file is in Amazon S3 as specified, and your knowledge transformation is now full.

Clear up

To keep away from incurring future prices, we advocate deleting the sources you created throughout this walkthrough.


On this put up, we walked via making use of customized SQL statements to an Amazon Redshift knowledge supply in your dataset, which you should use in profiling and transformation jobs. Now you can concentrate on constructing your knowledge transformation steps understanding that you simply’re engaged on solely the wanted knowledge.

To study extra in regards to the numerous supported knowledge sources for DataBrew, see Connecting to knowledge with AWS Glue DataBrew.

In regards to the authors

Suraj Shivananda is a Options Architect at AWS. He has over a decade of expertise in Software program Engineering, Knowledge and Analytics, DevOps particularly for knowledge options, automating and optimizing cloud based mostly options. He’s a trusted technical advisor and helps prospects construct Properly Architected options on the AWS platform.

Marie Yap is a Principal Options Architect for Amazon Net Providers based mostly in Hawaii. On this position, she helps numerous organizations start their journey to the cloud. She additionally makes a speciality of analytics and fashionable knowledge architectures.

Dhiraj Thakur is a Options Architect with Amazon Net Providers. He works with AWS prospects and companions to supply steering on enterprise cloud adoption, migration, and technique. He’s obsessed with know-how and enjoys constructing and experimenting within the analytics and AI/ML house.


Most Popular