Databricks DE Β· Topic 4.1
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.
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.βΈ "land" new files, then run Auto Loader; restart-safe via 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');
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".cloudFiles.useNotifications=true) β file events on UC external locations (recommended at scale; DBR 14.3 LTS+).| Mode | Behavior |
|---|---|
addNewColumns (default) | New column β stream fails (UnknownFieldException), schema updated; restart resumes. |
addNewColumnsWithTypeWidening | Same, plus widens types (e.g. intβlong). |
rescue | Never evolves, never fails β odd data β _rescued_data. |
failOnNewColumns | Fails and won't restart until you update the schema. |
none | Ignores 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")
| Directory listing | File notification | File events (UC) | |
|---|---|---|---|
| Setup | none | cloud queue resources | enable on UC ext. location |
| Cost at scale | high (list calls) | low | lowest |
| Recommended | small dirs | large dirs | default for scale |
_rescued_data.trigger(availableNow=True)) bronze ingestion from S3/ADLS/GCS/UC Volumes; JSON, CSV, Parquet, Avro, XMLβ¦addNewColumns fails-then-restarts on a new column (run under a retrying job); directory listing gets slow on huge dirs β file notification / file events; deleting the checkpoint reprocesses everything._rescued_data β that's where malformed rows go.