Sub topic 5
Ingesting files incrementally with Auto Loader
This chapter assumes you know basic SQL and PySpark, can work in a Databricks workspace with Unity Catalog, and understand the difference between batch and streaming data. It also assumes you know what a streaming table is from the previous chapter.
The problem Auto Loader solves
Raw data often arrives as files in cloud storage, such as JSON files dropped into a folder every few minutes. A plain batch read would scan the whole folder each time and reprocess old files. Auto Loader is a Databricks file source that tracks which files it has already seen and processes only new ones. In a pipeline, it is the usual way to fill a bronze table.
Auto Loader keeps this state in a checkpoint, which is a record of progress that the pipeline manages for you. You don't configure a checkpoint location in a pipeline.
Ingesting with SQL
In SQL, you use the read_files table-valued function inside a STREAM call. Auto Loader runs underneath it.
CREATE OR REFRESH STREAMING TABLE orders_bronze
AS SELECT *
FROM STREAM read_files(
'/Volumes/main/demo/raw/orders/',
format => 'json'
);Three parts matter here:
CREATE OR REFRESH STREAMING TABLEdefines a table that grows as new data arrives.STREAMmakes the read incremental. Without it, the query is a batch read.read_filestakes a path and aformat. The path here is a Unity Catalog volume.
Ingesting with Python
In Python, you use the cloudFiles source, which is the name of Auto Loader's streaming format.
import dlt
@dlt.table(comment="Raw orders ingested with Auto Loader")
def orders_bronze():
return (
spark.readStream.format("cloudFiles")
.option("cloudFiles.format", "json")
.load("/Volumes/main/demo/raw/orders/")
)The function returns a streaming DataFrame, and the decorator turns it into a dataset. The option cloudFiles.format names the format of the source files. Don't confuse it with the format("cloudFiles") call, which selects Auto Loader itself.
How a run behaves
Each update processes the files that arrived since the last update. If an update finds no new files, the table stays as it is. Files already processed are not read again, even if you run the pipeline many times.
A few points follow from this:
- Auto Loader decides by file path and state, not by content. If you overwrite an existing file with new content, it is usually not picked up again.
- A full refresh clears the table and its state, then reads every file again. Use it deliberately, because it reprocesses all history.
- Schema inference samples the files and stores the result. If new files add columns, Auto Loader can evolve the schema, but the exact behavior depends on your schema evolution settings.
Common errors
- Forgetting
STREAMin SQL. The query becomes a batch read, so the table is not incremental, and the table type may not match your intent. - Pointing at the wrong path. An empty or mistyped path gives an empty table or a path error. Check that the path exists and that your pipeline can read the volume.
- Wrong
format. If the files are CSV and you sayjson, the rows come out malformed or null. - Expecting edited files to reload. Write new files instead of changing old ones.
Exercise
A teammate wrote CREATE OR REFRESH STREAMING TABLE t AS SELECT * FROM read_files('/Volumes/main/demo/raw/orders/', format => 'json'); and finds the table is not ingesting incrementally. What is missing, and why does it matter?
Show answer
The STREAM keyword before read_files. Without it the query is a batch read, so it does not use Auto Loader's incremental file tracking. Adding STREAM makes each update read only files that arrived since the last update.
Exercise
You overwrite an already ingested file orders_001.json with corrected data, then run an update. Why are the corrections usually missing from the bronze table, and what are two ways to get them in?
Show answer
Auto Loader tracks files it has already processed and does not read them again. You can write the corrected rows as a new file, or run a full refresh to reprocess all files. The full refresh rereads all history, so use it with care.