Sub topic 3
Ingesting files incrementally with Auto Loader
This chapter assumes you know basic PySpark DataFrame operations, Delta Lake tables, and how to use notebooks, clusters and Unity Catalog volumes. It also assumes you have met the idea of a stream as an unbounded table from the earlier chapters.
What Auto Loader does
Auto Loader is a Databricks source for Structured Streaming. It reads new files from cloud storage as they arrive and tracks which files it has already processed. You use it by naming the cloudFiles format in readStream.
Compared with a plain file source, it keeps file discovery state in the checkpoint, so each file is ingested once. It can also infer the schema and store it for later runs.
A first Auto Loader query
The query below reads JSON files from a landing path and appends them to a Delta table. The availableNow trigger processes everything that is available and then stops, so you can run it as a scheduled job.
df = (
spark.readStream.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/demo/chk/orders_schema")
.load("/Volumes/main/demo/landing/orders")
)
query = (
df.writeStream
.option("checkpointLocation", "/Volumes/main/demo/chk/orders")
.trigger(availableNow=True)
.toTable("main.demo.orders_bronze")
)The pieces work as follows:
cloudFiles.formatis the format of the incoming files, here JSON.cloudFiles.schemaLocationis where Auto Loader stores the schema it inferred. It must be a path you can write to.checkpointLocationstores progress, including which files were processed.toTablewrites to a Unity Catalog Delta table and starts the query.
Schema inference
For JSON, Auto Loader infers column types as strings by default, unless you set cloudFiles.inferColumnTypes to true. On the first run it samples files and writes the inferred schema to the schema location. Later runs reuse that schema. The next chapters on schema handling cover what happens when new columns appear.
Common errors
- Missing schema location. Without
cloudFiles.schemaLocation, schema inference fails. Set it, or supply a schema yourself with.schema(...). - Sharing one checkpoint. Each query needs its own checkpoint path. Reusing one across queries corrupts progress tracking.
- Deleting the checkpoint to rerun. This makes Auto Loader forget which files it processed, so it ingests everything again and creates duplicates in an append-only table.
- Expecting changed files to reload. By default a file is processed once. If a file is overwritten with the same path, the new content is not picked up unless you change the relevant settings.
Choosing a trigger
With availableNow=True, the job behaves like an incremental batch. It is cheap, because the cluster can shut down after the run. If you need continuous low latency, use a processing-time trigger instead and keep the cluster running. The triggers chapter covers this choice.
Exercise
You delete the checkpoint folder of an Auto Loader query that appends to a Delta table, then run it again against the same landing path. What happens, and why?
Show answer
Auto Loader ingests every file in the landing path again. The checkpoint holds the record of processed files, so after it is deleted the query has no memory of earlier runs. The table then contains duplicate rows.
Exercise
Which two options does the example set that point to storage locations, and what does each store?
Show answer
cloudFiles.schemaLocation stores the inferred schema. checkpointLocation stores query progress, including which files were already processed.