Sub topic 3
Tracking Files and Tuning Auto Loader
Assume you know basic Python or SQL, Spark DataFrames, Structured Streaming, Delta tables, and cloud storage paths.
Tracking the Source File with `_metadata`
Auto Loader adds the _metadata column to each input row. Its fields include the source file path, file name, file size, and modification time. Store the path when you need to trace a record back to its input file.
from pyspark.sql.functions import col
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.load("/Volumes/main/raw/landing/orders")
.withColumn("source_file", col("_metadata.file_path")))The metadata column is produced by the file source. It is not part of the original JSON data.
Directory Listing Versus File Notifications
Auto Loader can discover files in two main ways:
| Mode | How files are found | Typical trade-off |
|---|---|---|
| Directory listing | Lists the storage path and compares files with its state | Simple, but repeated listing can become expensive |
| File notification | Uses cloud events or a managed queue of new files | Scales better, but needs notification setup |
Use directory listing for small sources or simple deployments. Use file notifications when the directory contains many files or receives files frequently.
Existing Files and File Notifications
On the first run, Auto Loader can process files already in the source directory. Its checkpoint records discovered files, so restarting the stream does not normally process completed files again.
Enable file notifications when supported by your deployment:
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "csv")
.option("header", "true")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/events")
.option("cloudFiles.useNotifications", "true")
.load("s3://my-bucket/landing/events"))Limiting Each Micro-batch
Use cloudFiles.maxFilesPerTrigger or cloudFiles.maxBytesPerTrigger to limit work per micro-batch. This controls load on the cluster and downstream systems. Limits are starting points, so measure processing delay and adjust them.
The SQL Equivalent
A streaming table can use read_files:
CREATE OR REFRESH STREAMING TABLE orders_bronze AS
SELECT *, _metadata.file_path AS source_file
FROM STREAM read_files(
'/Volumes/main/raw/landing/orders',
format => 'json'
);Production Practices
Keep schema and checkpoint locations stable and separate for each stream. Monitor input rate, processing delay, failures, and rescued records. Restart a failed stream with the same checkpoint. Do not delete its checkpoint unless you intentionally want to rebuild its processing state.
> callout note > Treat the checkpoint as operational state. Back it up or protect its storage permissions, and do not share it between unrelated streams. > ```
Exercise
Where should you look when a Delta row has an unexpected value?
Show answer
Check its source_file value and then inspect that input file.