Test drive data lineage (tutorial)
In this tutorial, you run an AWS Glue job that joins two tables and writes a summary table, and then you view the table-level and column-level lineage that AWS Glue captures. The tutorial takes about 20 minutes.
Prerequisites
-
An AWS account in a Region that supports AWS Glue Data Catalog Lineage. This tutorial uses
us-east-1. -
An Amazon S3 bucket for data, for example
amzn-s3-demo-bucket. -
An IAM role for AWS Glue that has the
AWSGlueServiceRolemanaged policy and read and write access to your bucket.AWSGlueServiceRoleincludesglue:PostLineageEvent. -
The AWS CLI.
Step 1: Create the database
aws glue create-database --database-input '{"Name":"lineage_tutorial"}' --region us-east-1
Step 2: Create an AWS Glue job
When you create tables in the Data Catalog, AWS Glue records lineage from each table's Amazon S3 location to the table.
-
Save the following script as
lineage_tutorial.pyand upload it tos3://amzn-s3-demo-bucket/scripts/.from pyspark.sql import SparkSession spark = SparkSession.builder.enableHiveSupport().getOrCreate() bucket = "s3://amzn-s3-demo-bucket/lineage_tutorial" spark.sql(f"""CREATE TABLE IF NOT EXISTS lineage_tutorial.orders (order_id int, customer_id int, amount double) USING parquet LOCATION '{bucket}/orders'""") spark.sql(f"""CREATE TABLE IF NOT EXISTS lineage_tutorial.customers (customer_id int, region string) USING parquet LOCATION '{bucket}/customers'""") spark.sql("INSERT INTO lineage_tutorial.orders VALUES (1, 10, 25.0), (2, 11, 40.0), (3, 10, 15.0)") spark.sql("INSERT INTO lineage_tutorial.customers VALUES (10, 'east'), (11, 'west')") spark.sql(f"""CREATE TABLE lineage_tutorial.region_totals USING parquet LOCATION '{bucket}/region_totals' AS SELECT c.region, sum(o.amount) AS total FROM lineage_tutorial.orders o JOIN lineage_tutorial.customers c ON o.customer_id = c.customer_id GROUP BY c.region""") -
Create the job with AWS Glue 6.1:
aws glue create-job --name lineage-tutorial \ --role arn:aws:iam::111122223333:role/MyGlueRole \ --glue-version 6.1 \ --command '{"Name":"glueetl","ScriptLocation":"s3://amzn-s3-demo-bucket/scripts/lineage_tutorial.py","PythonVersion":"3"}' \ --default-arguments '{"--enable-glue-datacatalog":"true"}' \ --region us-east-1
You don't need to set any lineage properties. AWS Glue adds the lineage listener when the job starts.
Step 3: Run the job
aws glue start-job-run --job-name lineage-tutorial --region us-east-1
Wait for the run to reach SUCCEEDED. Lineage processing is asynchronous and
usually completes within a few minutes after the job finishes.
Step 4: View the lineage graph
-
Open the AWS Glue console at https://console.aws.amazon.com/glue/
. -
In the navigation pane, choose Tables.
-
Choose
region_totals, and then choose the Lineage tab.
The graph shows orders and customers flowing into the job, and
the job writing region_totals.
Step 5: View column lineage
On the region_totals node, expand the columns. total is derived
from orders.amount, and region comes from
customers.region.
Step 6: Read lineage with the AWS CLI
aws glue get-lineage-node \ --identifier arn:aws:glue:us-east-1:111122223333:table/lineage_tutorial/region_totals \ --region us-east-1
The response includes the node ID, UpstreamNodes (the job), and
FormsOutput, which contains the column lineage.
Step 7: Clean up
aws glue delete-job --job-name lineage-tutorial --region us-east-1 aws glue delete-database --name lineage_tutorial --region us-east-1 aws s3 rm s3://amzn-s3-demo-bucket/lineage_tutorial --recursive