---
title: "Tutorial: Build an ETL pipeline with Apache Spark on the Databricks platform"
description: Learn how to create and deploy an ETL (extract, transform, and load) pipeline with Apache Spark on the Databricks platform.
canonical_url: https://docs.databricks.com/aws/en/getting-started/etl-quick-start
---

# Tutorial: Build an ETL pipeline with Apache Spark on the Databricks platform

This tutorial shows you how to develop and deploy your first ETL (extract, transform, and load) pipeline for data orchestration with Apache Spark. Although this tutorial uses Databricks all-purpose compute, you can also use serverless compute if it's enabled for your workspace.

You can also build ETL pipelines with [Lakeflow pipelines](https://docs.databricks.com/aws/en/getting-started/data-pipeline-get-started.md), which manage orchestration and streaming checkpoints. Lakeflow pipelines are built on Apache Spark™ Declarative Pipelines. With [serverless compute](https://docs.databricks.com/aws/en/ldp/serverless.md), Databricks also manages the pipeline's compute infrastructure.

By the end of this article, you will know how to:

1. [Launch a Databricks all-purpose compute resource](https://docs.databricks.com/aws/en/getting-started/etl-quick-start.md#cluster).
2. [Create a Databricks notebook](https://docs.databricks.com/aws/en/getting-started/etl-quick-start.md#notebook).
3. [Configure incremental data ingestion to Delta Lake with Auto Loader](https://docs.databricks.com/aws/en/getting-started/etl-quick-start.md#auto-loader).
4. [Process and interact with data](https://docs.databricks.com/aws/en/getting-started/etl-quick-start.md#process).
5. [Schedule a notebook as a Databricks job](https://docs.databricks.com/aws/en/getting-started/etl-quick-start.md#schedule).

This tutorial uses interactive notebooks to complete common ETL tasks in Python or Scala.

You can also use the [Databricks Terraform provider](https://docs.databricks.com/aws/en/dev-tools/terraform/index.md) to create this article's resources. See [Create clusters, notebooks, and jobs with Terraform](https://docs.databricks.com/aws/en/dev-tools/terraform/cluster-notebook-job.md).

## Requirements

* You are logged into a Databricks workspace.
* You have [permission to create a compute resource](https://docs.databricks.com/aws/en/compute/use-compute.md).

> [!NOTE]
>
> If you do not have compute control privileges, you can still complete most of the steps below as long as you have [access to a compute resource](https://docs.databricks.com/aws/en/compute/use-compute.md#permissions).

## <a id="cluster" />Step 1: Create a compute resource

To do exploratory data analysis and data engineering, create a compute resource to execute commands.

> [!NOTE]
>
> If your workspace is enabled for serverless compute, you can skip this step. Notebooks attach to serverless compute automatically on code execution, or you can select **Serverless** from the compute drop-down. See [Serverless compute for notebooks](https://docs.databricks.com/aws/en/compute/serverless/notebooks.md).

1. Click ![compute icon](https://docs.databricks.com/aws/en/images/icons/compute-icon.png) **Compute** in the sidebar.
2. On the Compute page, click **Create compute**.
3. Specify a unique name for the compute resource, leave the remaining values in their default state, and click **Create compute**.

To learn more about Databricks compute, see [Compute](https://docs.databricks.com/aws/en/compute/index.md).

## <a id="notebook" />Step 2: Create a Databricks notebook

To create a notebook in your workspace, click ![New Icon](https://docs.databricks.com/aws/en/images/icons/create-icon.png) **New** in the sidebar, and then click **Notebook**. A blank notebook opens in the workspace.

To learn more about creating and managing notebooks, see [Manage Databricks notebooks](https://docs.databricks.com/aws/en/notebooks/notebooks-manage.md).

## <a id="auto-loader" />Step 3: Configure Auto Loader to ingest data to Delta Lake

Databricks recommends using [Auto Loader](https://docs.databricks.com/aws/en/ingestion/cloud-object-storage/auto-loader/index.md) for incremental data ingestion. Auto Loader automatically detects and processes new files as they arrive in cloud object storage.

Databricks recommends storing data with [Delta Lake](https://docs.databricks.com/aws/en/delta/index.md). Delta Lake is an open source storage layer that provides ACID transactions and enables the data lakehouse. Delta Lake is the default format for tables created in Databricks.

To configure Auto Loader to ingest data to a Delta Lake table, copy and paste the following code into the empty cell in your notebook:

### Python

```python
# Import functions
from pyspark.sql.functions import col, current_timestamp

# Define variables used in code below
file_path = "/databricks-datasets/structured-streaming/events"
username = spark.sql("SELECT regexp_replace(session_user(), '[^a-zA-Z0-9]', '_')").first()[0]
table_name = f"{username}_etl_quickstart"
checkpoint_path = f"/tmp/{username}/_checkpoint/etl_quickstart"

# Clear out data from previous demo execution
spark.sql(f"DROP TABLE IF EXISTS {table_name}")
dbutils.fs.rm(checkpoint_path, True)

# Configure Auto Loader to ingest JSON data to a Delta table
(spark.readStream
  .format("cloudFiles")
  .option("cloudFiles.format", "json")
  .option("cloudFiles.schemaLocation", checkpoint_path)
  .load(file_path)
  .select("*", col("_metadata.file_path").alias("source_file"), current_timestamp().alias("processing_time"))
  .writeStream
  .option("checkpointLocation", checkpoint_path)
  .trigger(availableNow=True)
  .toTable(table_name))
```

### Scala

```scala
// Imports
import org.apache.spark.sql.functions.current_timestamp
import org.apache.spark.sql.streaming.Trigger
import spark.implicits._

// Define variables used in code below
val file_path = "/databricks-datasets/structured-streaming/events"
val username = spark.sql("SELECT regexp_replace(session_user(), '[^a-zA-Z0-9]', '_')").first.get(0)
val table_name = s"${username}_etl_quickstart"
val checkpoint_path = s"/tmp/${username}/_checkpoint"

// Clear out data from previous demo execution
spark.sql(s"DROP TABLE IF EXISTS ${table_name}")
dbutils.fs.rm(checkpoint_path, true)

// Configure Auto Loader to ingest JSON data to a Delta table
spark.readStream
  .format("cloudFiles")
  .option("cloudFiles.format", "json")
  .option("cloudFiles.schemaLocation", checkpoint_path)
  .load(file_path)
  .select($"*", $"_metadata.file_path".as("source_file"), current_timestamp.as("processing_time"))
  .writeStream
  .option("checkpointLocation", checkpoint_path)
  .trigger(Trigger.AvailableNow)
  .toTable(table_name)
```

> [!NOTE]
>
> The variables defined in this code should allow you to safely execute it without risk of conflicting with existing workspace assets or other users. Restricted network or storage permissions will raise errors when executing this code; contact your workspace administrator to troubleshoot these restrictions.

To learn more about Auto Loader, see [What is Auto Loader?](https://docs.databricks.com/aws/en/ingestion/cloud-object-storage/auto-loader/index.md).

## <a id="process" />Step 4: Process and interact with data

Notebooks execute logic cell-by-cell. To execute the logic in your cell:

1. To run the cell you completed in the previous step, select the cell and press **SHIFT+ENTER**.

2. To query the table you've just created, copy and paste the following code into an empty cell, then press **SHIFT+ENTER** to run the cell.

   ### Python

   ```python
   df = spark.read.table(table_name)
   ```

   ### Scala

   ```scala
   val df = spark.read.table(table_name)
   ```

3. To preview the data in your DataFrame, copy and paste the following code into an empty cell, then press **SHIFT+ENTER** to run the cell.

   ### Python

   ```python
   display(df)
   ```

   ### Scala

   ```scala
   display(df)
   ```

To learn more about interactive options for visualizing data, see [Visualizations in Databricks notebooks and SQL editor](https://docs.databricks.com/aws/en/visualizations/index.md).

## <a id="schedule" />Step 5: Schedule a job

You can run Databricks notebooks as production scripts by adding them as a task in a Databricks job. In this step, you will create a new job that you can trigger manually.

> [!NOTE]
>
> If you are using serverless compute, select **Serverless** in the **Compute** drop-down instead of the compute resource from Step 1.

To schedule your notebook as a task:

1. Click **Schedule** on the right side of the header bar.
2. Enter a unique name for the **Job name**.
3. Click **Manual**.
4. In the **Compute** drop-down, select the compute resource you created in step 1.
5. Click **Create**.
6. In the window that appears, click **Run now**.
7. To see the job run results, click the ![External Link](https://docs.databricks.com/aws/en/images/icons/external-link.png) icon next to the **Last run** timestamp.

For more information on jobs, see [What are jobs?](https://docs.databricks.com/aws/en/jobs/index.md#jobs).

## Additional integrations

Learn more about integrations and tools for data engineering with Databricks:

* [Tutorial: Build an ETL pipeline with Lakeflow pipelines](https://docs.databricks.com/aws/en/getting-started/data-pipeline-get-started.md)
* [Developer tools use cases](https://docs.databricks.com/aws/en/dev-tools/index.md)
* [Connect to dbt Core](https://docs.databricks.com/aws/en/partners/prep/dbt.md)
* [Databricks CLI](https://docs.databricks.com/aws/en/dev-tools/cli/index.md)
* [Databricks Terraform provider](https://docs.databricks.com/aws/en/dev-tools/terraform/index.md)
