Sub topic 1
Why incremental ingestion needs more than listing a directory
Incremental ingestion means processing only the files that arrived since the last run. This chapter explains why the obvious way to do that, listing a directory and comparing it to what you have already loaded, stops working as data grows. It also introduces what Auto Loader does in its place.
Prerequisites
This chapter assumes you have:
- Basic Python or SQL.
- Familiarity with Spark DataFrames.
- A basic idea of Structured Streaming (
readStreamandwriteStream). - Familiarity with Delta tables and cloud object storage paths.
The naive approach
Suppose an upstream system drops JSON files into /Volumes/main/raw/landing/orders all day. The simplest job reads the whole folder on every run:
df = spark.read.format("json").load("/Volumes/main/raw/landing/orders")
df.write.mode("append").saveAsTable("main.bronze.orders")This has a correctness problem and a cost problem. Every run re-reads every file, so each run appends all earlier rows again. Duplicates appear in the table.
The manual fix and its limits
A common fix is to keep a record of processed files. Each run lists the directory, removes the files already recorded, and reads only the rest.
processed = {r.path for r in spark.table("main.ops.processed_files").collect()}
all_files = [f.path for f in dbutils.fs.ls("/Volumes/main/raw/landing/orders")]
new_files = [p for p in all_files if p not in processed]This works for a few thousand files. It breaks down for these reasons:
- Listing cost grows with history. Object storage has no cheap "what changed" query. A listing call returns a limited page of results, so a folder with millions of files needs many sequential requests. The run time depends on the total number of files, not on the number of new ones.
- The bookkeeping is yours. If the job fails between loading the data and updating the list, the next run either loads some files twice or skips them.
- The list itself grows. Comparing millions of paths on every run adds memory and compute cost.
- Schema changes are not handled. A new column in a file can fail the read or be dropped without warning.
What an incremental source changes
An incremental source moves the bookkeeping out of your code and into the engine. It must do three things:
- Find new files without rescanning the whole history on each run.
- Remember which files it has processed, durably, so a restart does not repeat or skip work.
- Keep working when the data's shape changes.
Databricks Auto Loader does this through a Structured Streaming source named cloudFiles. It stores discovered files in a checkpoint, which is a state store kept in cloud storage. It can also discover files through cloud event notifications instead of listing. The following chapters cover each part.
A minimal version looks like this:
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"))You do not need to understand every option yet. Notice what is missing: there is no list of processed files and no comparison step. The checkpoint holds that state.
Common mistakes to avoid
- Treating a plain
spark.readof a landing folder as incremental. It reprocesses everything. - Keeping a hand-built processed-files table next to Auto Loader. The checkpoint already does this job, and two sources of truth can disagree.
- Deleting the checkpoint to "fix" a problem. The stream then forgets what it processed and reads the folder from the start.
Exercise
A job lists a landing folder on each run and skips paths found in a processed-files table. The folder now holds 5 million files and runs take hours, even when only 100 files are new. Name two reasons the design scales poorly and what an incremental source stores instead.
Show answer
Run time depends on the total file count because listing must page through every file, and comparing the full list with the processed set grows in cost too. The manual table can also fall out of step with the data if a run fails midway, causing duplicates or skipped files. An incremental source such as Auto Loader stores the discovered and processed files in a checkpoint that is updated together with the progress of the stream.