

# 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
<a name="catalog-lineage-tutorial-prerequisites"></a>
+ 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 `AWSGlueServiceRole` managed policy and read and write access to your bucket. `AWSGlueServiceRole` includes `glue:PostLineageEvent`.
+ The AWS CLI.

## Step 1: Create the database
<a name="catalog-lineage-tutorial-step-1-create-the-database"></a>

```bash
aws glue create-database --database-input '{"Name":"lineage_tutorial"}' --region us-east-1
```

## Step 2: Create an AWS Glue job
<a name="catalog-lineage-tutorial-step-2-create-an-aws-glue-job"></a>

When you create tables in the Data Catalog, AWS Glue records lineage from each table's Amazon S3 location to the table.

1. Save the following script as `lineage_tutorial.py` and upload it to `s3://amzn-s3-demo-bucket/scripts/`.

   ```python
   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""")
   ```

1. Create the job with AWS Glue 6.1:

   ```bash
   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
<a name="catalog-lineage-tutorial-step-3-run-the-job"></a>

```bash
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
<a name="catalog-lineage-tutorial-step-4-view-the-lineage-graph"></a>

1. Open the AWS Glue console at [https://console.aws.amazon.com/glue/](https://console.aws.amazon.com/glue/).

1. In the navigation pane, choose **Tables**.

1. 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
<a name="catalog-lineage-tutorial-step-5-view-column-lineage"></a>

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
<a name="catalog-lineage-tutorial-step-6-read-lineage-with-the-aws-cli"></a>

```bash
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
<a name="catalog-lineage-tutorial-step-7-clean-up"></a>

```bash
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
```