7.4 C
New York
Tuesday, March 21, 2023

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

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

DataBrew is a visible information preparation software that may provide help to 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 part of, and filter the information earlier than cleansing, normalizing, and reworking it in a DataBrew undertaking. Filtering and becoming a member of the information out of your information supply and solely bringing within the information you need to remodel simplifies the ETL course of.

On this publish, we exhibit use customized SQL queries to outline your Amazon Redshift datasets in DataBrew.

Answer overview

To implement this resolution, 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 information.
  4. Create a DataBrew undertaking and recipe job to rework the information and cargo it to Amazon Easy Storage Service (Amazon S3).

The next diagram illustrates the structure for our resolution.


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

  1. Have an AWS account.
  2. Create an Amazon Redshift cluster in a personal subnet inside a VPC as a safety finest observe.
  3. As a result of DataBrew instructions require that the cluster has entry to Amazon S3, be sure to 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 machine 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 information within the Amazon Redshift cluster.
  6. Put together a SQL question to extract the supply dataset. You employ this SQL question later on this publish to create an Amazon Redshift supply dataset in DataBrew.
  7. Create an S3 bucket to retailer information from the profile and recipe jobs. The DataBrew connection briefly shops intermediate information in Amazon S3.
  8. For our use case, we use a mock dataset. You’ll be able to obtain the DDL and information recordsdata from GitHub.

Safety finest practices

Take into account the next finest practices to be able to mitigate safety threats:

  • Overview the shared accountability mannequin when utilizing DataBrew.
  • Limit community entry for inbound and outbound site visitors to least privilege. Reap the benefits of the routing site visitors throughout the VPC by utilizing an Amazon S3 gateway endpoint and enhanced VPC routing in Amazon Redshift.
  • Allow the lifecycle coverage in Amazon S3 to retain solely crucial information, and delete pointless information.
  • Allow Amazon S3 versioning and cross-Area replication for essential datasets to guard in opposition to 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 must 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 title, enter a reputation, reminiscent of order-db-connection.
  4. For Connection sort, choose Amazon Redshift.
  5. Underneath Connection entry, present the Amazon Redshift cluster title, database title, database person, and database password.
  6. Select Create connection.

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

On this part, you create a Amazon Redshift connection, add your customized SQL assertion, and validate it. You can too validate your SQL assertion immediately in your Amazon Redshift cluster by utilizing the Amazon Redshift question editor v2. The aim of validating the SQL assertion is that can assist you keep away from failure in loading your dataset right into a undertaking or job. Additionally, checking the question runtime ensures that it runs in below 3 minutes, avoiding timeouts throughout undertaking 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 title, 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 briefly retailer the intermediate outcomes.
  8. Select Create dataset.

Create a DataBrew profile job

On this part, you utilize the newly created Amazon Redshift dataset to create a profile job. Information profiling helps you perceive your dataset and plan the information 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 title, 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 Information pattern, choose Full dataset.
  7. Underneath Job output settings¸ for S3 location, enter the S3 bucket for the job output recordsdata.
  8. For Position title, select an AWS Id and Entry Administration (IAM) position with permission for DataBrew to hook up with the information in your behalf. For extra info, discuss with Including an IAM position with information 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’ll be able to select View information profile to see extra info.

Along with an output file, DataBrew additionally gives visualizations. On the Dataset profile overview tab, you’ll be able to see information visualizations that may provide help to perceive your information higher. Subsequent, you’ll be able to see detailed statistics about your information on the Column statistics tab, illustrated with graphics and charts. You’ll be able to outline information high quality guidelines on the Information high quality guidelines tab, after which see the outcomes from the information 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’ll be able to present guidelines that keep away from triggering a recipe job in case of an anomaly. You can too notify the supply groups to deal with or acknowledge the lacking values. DataBrew customers may add steps within the recipe job to deal with the anomalies and lacking values.

Create a DataBrew undertaking and recipe job

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

Create a undertaking

To create your undertaking, full the next steps:

  1. On the DataBrew console, select Initiatives within the navigation pane.
  2. Select Create undertaking.
  3. For Undertaking title, enter a reputation, reminiscent of order-data-proj.
  4. Underneath Recipe particulars¸ select Create new recipe and enter a recipe title, 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 title, select your DataBrew position.
  8. Select Create undertaking.

DataBrew begins a session, constructs a DataFrame, extracts pattern information, infers primary statistics, and shows the pattern information in a grid view. You’ll be able to add steps to construct a change recipe. As of this writing, DataBrew gives 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 an inventory 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 title¸ 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 title, select the IAM position that comprises permissions for DataBrew to attach in your behalf.
  7. Select Create and run job.

You’ll be able to verify the standing of your job on the Jobs web page.

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

Clear up

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


On this publish, we walked by way of making use of customized SQL statements to an Amazon Redshift information supply in your dataset, which you should use in profiling and transformation jobs. Now you can give attention to constructing your information transformation steps understanding that you just’re engaged on solely the wanted information.

To be taught extra concerning the varied supported information sources for DataBrew, see Connecting to information 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, Information and Analytics, DevOps particularly for information options, automating and optimizing cloud based mostly options. He’s a trusted technical advisor and helps clients construct Nicely Architected options on the AWS platform.

Marie Yap is a Principal Options Architect for Amazon Net Companies based mostly in Hawaii. On this position, she helps varied organizations start their journey to the cloud. She additionally focuses on analytics and trendy information architectures.

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

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles