What Databricks Auto Loader solves
Data engineers who ingest files from cloud storage like ADLS Gen2, Amazon S3 or Google Cloud Storage need three things: pick up every new file, skip files that are already loaded, and keep the code simple. Databricks Auto Loader handles all three. In this demo, Green Leaf Data Architect Kiran Lokhande uses Auto Loader with PySpark on Azure Databricks to ingest CSV files, handle schema changes, move processed files and write the results to a Delta table.
You can use Auto Loader in two ways: through Lakeflow Spark Declarative Pipelines (formerly Delta Live Tables) or directly in PySpark. This demo uses PySpark, which gives you full control over the configuration.
Directory listing mode vs file events mode (1:22)
Auto Loader can discover new files in two ways.
- Directory listing mode scans the folder on a regular schedule to look for new files. It’s simple and works well when you have a small number of files, but it gets expensive as file volume grows.
- File events mode is the recommended option at scale. It relies on cloud storage notifications so Auto Loader doesn’t have to keep scanning folders.
How file events mode works (2:16)
When a new file lands in cloud storage, the storage service sends an event to the cloud’s event broker, which publishes it to a queue. On the Databricks side, a file events service for the whole workspace reads from that queue and caches metadata about each new file, including which folder it’s in.
Each Auto Loader stream checks that cache for new files in its own path. The first time a stream runs, Auto Loader does a full directory listing to find existing files. After that, it relies on the cache, with an occasional directory listing to make sure nothing was missed.
Supported file formats (5:13)
Auto Loader can ingest JSON, CSV, XML, Parquet, Avro, ORC, text and binary files.
Step 1: Set up storage containers (6:17)
In the Azure storage account, Kiran creates two containers:
- Source container with two folders: a data folder where new files land, and a processed folder where files move after ingestion
- Sink container with two folders: one for the schema location and one for the checkpoint location
If you use Auto Loader through Lakeflow pipelines, Databricks manages these locations for you, so you don’t need to create them yourself.
Step 2: Create external locations (8:28)
In the Databricks Catalog, Kiran creates an external location for each container, using an existing storage credential. This gives Databricks secure read and write access to both containers.
Step 3: Understand streaming DataFrames (10:30)
Under the hood, Auto Loader uses a streaming DataFrame. A regular DataFrame works with a fixed batch of data. A streaming DataFrame represents data that keeps arriving: it watches a folder and processes new data incrementally. It needs a write stream to run, and it keeps running until you stop it, unless you set it to trigger once.
Step 4: Know why checkpoint and schema locations matter (11:59)
- Checkpoint location: stores the stream’s progress and state so Spark can pick up where it left off. Without it, a stream can’t recover from failures, data may be processed twice, and exactly-once guarantees break.
- Schema location: stores the inferred schema of incoming files and tracks how it changes over time. This is what makes schema evolution possible.
Step 5: Configure the read stream (13:41)
In a new notebook, Kiran builds the read stream with only a few settings:
- The cloud files format, set to CSV, with headers turned on
- Schema evolution set to rescue mode, so unexpected columns go to a rescued data column
- The schema location in the sink container
- A clean-up setting that moves processed files to the processed folder after a short retention period. You can also have Auto Loader delete them instead.
Step 6: Write the stream to a Delta table (21:16)
The write stream sends the data to a Delta table, points to the checkpoint location, merges schema changes into the table, and uses a trigger-once setting because the demo runs on serverless compute.
Step 7: Run it and ingest the first file (24:15)
The first run fails with an error saying Auto Loader can’t infer a schema from an empty folder. Once Kiran uploads the first CSV file of agriculture data and runs the stream again, Auto Loader creates the Delta table. It includes a rescued data column, which is empty for now because the file matches the inferred schema.
Step 8: See schema evolution in rescue mode (27:22)
The second file has two extra columns. Rather than failing the stream or changing the table, Auto Loader puts those values in the rescued data column. The row count doubles, and the first file isn’t reprocessed, because the checkpoint shows it was already ingested.
In the sink container, the checkpoint folder now holds commits, offsets and source metadata. The schema folder holds a record of each schema version.
Step 9: Switch to add new columns (30:25)
Next, Kiran changes the schema evolution mode to add new columns and uploads a third file with a different new column. On serverless, the stream stops once when it detects the new field. Running it again finishes the load. The new column appears in the table, empty for earlier rows and filled in for the new ones, and the rescued data column stays empty.
Step 10: Confirm files were moved (34:32)
After a few runs, all of the ingested files are in the processed folder, and the data folder is empty. The move can take more than one run because of the retention period.
A note on triggers and serverless (36:07)
On classic compute, you can set the stream to check for new files on an interval, such as every second, and leave it running. Serverless compute doesn’t support that trigger type, which is why the demo uses trigger once.
Step 11: Review the schema history (36:59)
The schema folder keeps a JSON file for each schema version. Opening the latest one shows the new column added in the third file, so you have a full record of how the source schema has changed.
Key takeaways
- Auto Loader ingests only new files. Checkpoints track what’s already been processed, so files are never loaded twice or missed.
- File events mode scales better than directory listing. It uses cloud storage notifications instead of repeated folder scans.
- Schema evolution keeps streams running. Rescue mode captures unexpected fields in one column, and add new columns mode updates the table automatically.
Frequently asked questions
What is Databricks Auto Loader?
Auto Loader is a Databricks feature that incrementally ingests new files from cloud storage into the lakehouse, without reprocessing files that were already loaded.
What is the difference between directory listing and file events mode?
Directory listing scans the folder regularly for new files, which works for small volumes. File events mode uses cloud storage notifications to find new files and is recommended at scale.
What happens when a new file has extra columns?
It depends on the schema evolution mode. Rescue mode puts unexpected fields in a rescued data column. Add new columns mode adds them to the table.
Why does Auto Loader need a checkpoint location?
The checkpoint stores the stream’s progress so it can recover from failures and avoid processing the same data twice.
Can I use Auto Loader on serverless compute?
Yes, but serverless supports trigger once rather than interval-based triggers, so you run the stream each time you want to process new files.
Want help building scalable ingestion on Databricks? Talk to Green Leaf’s Data Platform & Strategy team.
Watch the full discussion demo below.