Sub topic 1
Incremental File Ingestion with Databricks Auto Loader
Prerequisites
Assume you know basic Python, Spark DataFrames, readStream, writeStream, Delta tables, and cloud storage paths.
Why incremental ingestion needs more than listing a directory
A simple file stream can list a directory to find new files. As the directory grows, each run may scan more object-storage metadata. This increases latency and API cost. Repeated scans can also make large backfills and concurrent writers harder to manage.
| Approach | Work required as the directory grows | Main concern |
|---|---|---|
| Repeated directory listing | Scan more file names on each run | Slow discovery and higher metadata cost |
| Auto Loader | Track discovered files incrementally | Requires persistent state |
Auto Loader uses the cloudFiles streaming source. It records file-discovery state and presents newly discovered files to Spark as streaming input.
Reading files with the `cloudFiles` source
This example reads JSON files and appends them to a Delta table:
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.load("/Volumes/main/raw/landing/orders"))
(df.writeStream
.option("checkpointLocation", "/Volumes/main/raw/_checkpoints/orders")
.trigger(availableNow=True)
.toTable("main.bronze.orders"))The schema location stores inferred-schema state. The checkpoint stores streaming progress. Keep these paths persistent and separate for each logical stream.
How checkpoints give you each file exactly once
A checkpoint is durable state for a streaming query. After a micro-batch succeeds, Auto Loader and the sink record the files included in that progress. On restart, the query resumes from the checkpoint instead of treating every file as new.
With a Delta sink and a stable checkpoint, successfully processed files are not appended again. Changing or deleting the checkpoint can cause files to be processed again.
Running continuously or as an availableNow job
Choose a trigger based on how quickly data must appear:
| Trigger | Behavior | Typical use |
|---|---|---|
| Default or processing-time | Keeps checking for new files | Continuous ingestion |
| `availableNow=True` | Processes pending files, then stops | Scheduled batch-style jobs |
availableNow lets a scheduler start the stream periodically without maintaining a continuously running cluster.
Exercise
What happens if an Auto Loader stream restarts with its original checkpoint?
Show answer
It resumes from recorded progress and skips files that were already committed successfully.