Sub topic 5
Ingesting files into bronze with Auto Loader
This chapter assumes you know basic PySpark DataFrames, can read JSON files, and have seen the bronze layer idea: raw, append-only tables that keep data replayable. It also assumes you can run notebooks in a Unity Catalog workspace.
What Auto Loader does
Auto Loader is a Structured Streaming source named cloudFiles. It watches a storage location, finds files it has not seen, and passes them to your query as a streaming DataFrame. You do not list directories yourself, and you do not track which files you already loaded. Auto Loader records that state in the checkpoint.
For bronze, this fits well. Files arrive in a landing area, and each file should reach the bronze table exactly once.
A bronze ingestion step
The code below reads JSON from a volume, adds two metadata columns, and appends to a bronze table.
from pyspark.sql import functions as F
raw = (
spark.readStream.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/sales/checkpoints/orders_schema")
.load("/Volumes/main/sales/landing/orders/")
.withColumn("_ingested_at", F.current_timestamp())
.withColumn("_source_file", F.col("_metadata.file_path"))
)
(
raw.writeStream
.option("checkpointLocation", "/Volumes/main/sales/checkpoints/orders_bronze")
.trigger(availableNow=True)
.toTable("main.bronze.orders")
)Here is what each part does:
cloudFiles.formattells Auto Loader the file type to parse.cloudFiles.schemaLocationis where Auto Loader stores the schema it infers. It also stores later schema changes there._metadata.file_pathis a hidden column that holds the full path of the source file. Saving it as_source_filelets you trace any bronze row back to its file._ingested_atrecords when the row was loaded. The silver step uses it later to pick the latest record per key.checkpointLocationstores which files were processed. If the job stops and restarts, it resumes without reloading files.availableNow=Trueprocesses everything that is waiting, then stops. This suits scheduled jobs.
Schema handling
JSON has no fixed schema, so Auto Loader infers one by sampling files. By default it infers JSON columns as strings, which avoids type conflicts at the bronze layer. You cast to proper types in silver.
When a new field appears in the files, Auto Loader's default schema evolution mode, addNewColumns, stops the stream with an error. It first writes the new column to the schema location. When you restart the job, the new column is included. Data that does not match the schema can be kept in the _rescued_data column instead of being lost. This keeps bronze faithful to the source.
Common errors
- Sharing a checkpoint between queries. Each stream needs its own checkpoint path. Note that the schema location and the checkpoint above are different folders.
- Deleting the checkpoint to "fix" a problem. The next run sees every file as new and loads duplicates into bronze.
- Expecting the run to continue after a schema change. The first failure is expected. Rerun the job.
- Cleaning data in the bronze step. Keep bronze raw. Add metadata only, and do cleaning in silver.
Exercises
Exercise
A job using Auto Loader fails after a source system adds a new field to its JSON files. With the default settings, what should you do, and why is the data not lost?
Show answer
Rerun the job. Auto Loader saved the new column to the schema location before it stopped, so the next run includes the field. Any data that did not fit the schema is kept in _rescued_data.
Exercise
Why does the bronze query add _source_file and _ingested_at, and which later step uses _ingested_at?
Show answer
They give each row its origin and load time, so you can trace and replay data. The silver MERGE step orders by _ingested_at to keep the latest record per order_id.