Sub topic 11
Handling files that already exist
This chapter assumes you can write a basic readStream and writeStream job. It also assumes you know what a checkpoint is and how Auto Loader discovers files. Here we look at what happens on the first run, when the landing folder already holds files.
What the first run does
Auto Loader does not start at "now". When a stream starts with an empty checkpoint, it treats every file already under the input path as new. This is the initial backfill. The stream processes all existing files, then keeps processing files that arrive later.
The backfill uses the same machinery as normal ingestion. Each file is recorded in the checkpoint as it is processed. If the job fails halfway through a large backfill, a restart continues from the checkpoint. It does not reread files it has already committed.
Backfill size matters
A folder with millions of files makes the first run much larger than any later run. Two settings keep it manageable:
- Use
cloudFiles.maxFilesPerTriggerorcloudFiles.maxBytesPerTriggerso each micro-batch stays a predictable size. - Run the first load with the
availableNowtrigger, so it finishes and stops. You can then switch to a regular schedule.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.option("cloudFiles.maxFilesPerTrigger", "1000")
.load("/Volumes/main/raw/landing/orders"))
(df.writeStream
.option("checkpointLocation", "/Volumes/main/raw/_checkpoints/orders")
.trigger(availableNow=True)
.toTable("main.bronze.orders"))With this setup, a backfill of 100,000 files runs as about 100 micro-batches of 1,000 files each.
Skipping old files
Sometimes you do not want the history. Auto Loader has an option for this:
| Option | Effect |
|---|---|
| `cloudFiles.includeExistingFiles` | When `true` (the default), files already in the path are processed on the first run. When `false`, only files that arrive after the stream first starts are processed. |
| `cloudFiles.maxFileAge` | Tells Auto Loader how long to keep file records in the checkpoint. It is a tuning option for very high-volume streams, not a filter for old data. |
Set includeExistingFiles to false only when you are sure the history is not needed or is loaded another way. The setting is read when the stream first starts. Changing it later, on a checkpoint that already exists, has no effect.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "csv")
.option("header", "true")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/events")
.option("cloudFiles.includeExistingFiles", "false")
.load("/Volumes/main/raw/landing/events"))Common errors
- Backfill is slow or runs out of memory. The first batch tried to read too many files. Add a rate limit.
- Old files were missed. The stream was first started with
includeExistingFilesset tofalse. Files that were present at that time were never recorded. To load them, start a new stream with a new checkpoint, or load them with a separate one-time batch job. - Files were loaded twice. This usually happens after you delete or change the checkpoint location. An empty checkpoint means "everything is new" again. The target table then receives the existing files a second time.
Reloading on purpose
To reprocess everything, you have two choices. You can clear the target table and use a new checkpoint location. You can also write to a new table with a new checkpoint. In both cases, use a new schema location too, so that schema inference starts fresh.
Exercise
A stream was started for the first time with cloudFiles.includeExistingFiles set to false. Two weeks later you decide the 50,000 files that were already in the folder at that time are needed. You change the option to true and restart the stream with the same checkpoint. Are the old files loaded? What should you do instead?
Show answer
No. The option is only read when the stream first starts, and the checkpoint already exists, so the change has no effect. Load the old files with a one-time batch job, or start a new stream with a new checkpoint (and, if needed, a new schema location) and deal with any duplicates in the target table.