Databricks DE Β· Topic 4.1

Auto Loader (cloudFiles)

Incrementally ingests only the new files landing in cloud storage β€” a Structured Streaming source with checkpointing, schema evolution, and a rescued-data column. This deep dive walks each with the code.

cloudFilesexactly-onceschema evolution_rescued_datafile events

The one-liner

Auto Loader (format("cloudFiles")) streams in only new files as they arrive β€” checkpointed for exactly-once, with automatic schema inference/evolution and a _rescued_data column for mismatches.
🎒 Analogy: a conveyor belt that only picks up new boxes. Files keep landing in a folder; Auto Loader grabs just the ones it hasn't seen β€” never re-handling old boxes, even after a restart.

Incremental ingest β€” only new files

β–Έ "land" new files, then run Auto Loader; restart-safe via checkpoint

No files yet.

1 Β· Core setup β€” cloudFiles source + checkpoint

trigger(availableNow=True) processes all currently-available files then stops (ideal for scheduled jobs); omit for an always-on stream. Throttle with maxFilesPerTrigger/maxBytesPerTrigger.

PySpark

df = (spark.readStream.format("cloudFiles")
        .option("cloudFiles.format", "json")
        .option("cloudFiles.schemaLocation", "/Volumes/cat/sch/vol/_schema")
        .load("/Volumes/cat/sch/vol/landing"))

(df.writeStream
   .option("checkpointLocation", "/Volumes/cat/sch/vol/_ckpt")   # exactly-once state
   .trigger(availableNow=True)                                   # all new, then stop
   .toTable("cat.sch.bronze_events"))

SQL β€” read_files streaming TVF

CREATE OR REFRESH STREAMING TABLE cat.sch.bronze_events AS
SELECT * FROM STREAM read_files('/Volumes/cat/sch/vol/landing', format => 'json');

2 Β· Built-in features β€” click each

Schema inference & evolution β€” inferred schema stored at schemaLocation; behavior set by cloudFiles.schemaEvolutionMode (default addNewColumns: fail with UnknownFieldException, then restart resumes with the new column). Pin types with schemaHints; enable inferColumnTypes.
_rescued_data β€” values that don't match the schema (missing col, type/case mismatch) are preserved here instead of dropped. Rename via cloudFiles.rescuedDataColumn. Check it when rows look "missing".
File detection: directory listing (default) β†’ file notification (cloudFiles.useNotifications=true) β†’ file events on UC external locations (recommended at scale; DBR 14.3 LTS+).
Checkpoint β€” seen-file metadata in RocksDB β†’ exactly-once, restart-safe. One checkpoint per stream; deleting it reprocesses everything.

3 Β· Schema evolution modes

ModeBehavior
addNewColumns (default)New column β†’ stream fails (UnknownFieldException), schema updated; restart resumes.
addNewColumnsWithTypeWideningSame, plus widens types (e.g. int→long).
rescueNever evolves, never fails β€” odd data β†’ _rescued_data.
failOnNewColumnsFails and won't restart until you update the schema.
noneIgnores new columns; does not rescue.
.option("cloudFiles.schemaEvolutionMode", "addNewColumns")
.option("cloudFiles.schemaHints", "id long, event_ts timestamp")
.option("cloudFiles.inferColumnTypes", "true")
.option("cloudFiles.rescuedDataColumn", "_rescued")
πŸ’‘ The default fail-then-restart is intentional (clean, auditable schema change). Run under a job with retries so the restart is automatic in production.

Detection modes at a glance

Directory listingFile notificationFile events (UC)
Setupnonecloud queue resourcesenable on UC ext. location
Cost at scalehigh (list calls)lowlowest
Recommendedsmall dirslarge dirsdefault for scale

Why it matters

πŸ–±οΈ Real-world use case: clickstream JSON lands in S3 every few minutes. Auto Loader streams only new files into a bronze Delta table, evolving the schema as fields appear and parking malformed records in _rescued_data.

Uses, edge cases & limitations

Common gotchas