Sub topic 9
Tracking the source file with _metadata
Every row you ingest with Auto Loader came from a specific file. Recording that file lets you debug bad rows, audit loads, and reprocess a single file's data. This chapter shows how to capture it with the _metadata column.
This chapter assumes you can already write a basic cloudFiles stream, as covered earlier. You should also know how to read a Delta table.
What the _metadata column is
_metadata is a hidden column that Spark file-based sources expose, including Auto Loader. It is a struct (a column holding named fields) describing the file each row was read from. It does not appear in SELECT * and it is not part of your inferred schema. You have to ask for it by name.
The commonly used fields are:
| Field | Meaning |
|---|---|
| `file_path` | Full path of the source file |
| `file_name` | File name only |
| `file_size` | Size of the file in bytes |
| `file_modification_time` | When the file was last modified |
Some runtimes expose additional fields, such as block information. Check the documentation for your runtime version.
Adding it to your stream
Select the field you want and give it a column name. Copying the value into a regular column is what makes it persist in the target table.
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"))
.withColumn("source_modified", col("_metadata.file_modification_time")))
(df.writeStream
.option("checkpointLocation", "/Volumes/main/raw/_checkpoints/orders")
.trigger(availableNow=True)
.toTable("main.bronze.orders"))After the run, each row in main.bronze.orders carries the path and modification time of the file it came from.
Why this is worth doing
- Debugging. When a row has a bad value, you can find the exact file that produced it.
- Auditing. You can count rows per file and compare against what the sender claims to have delivered.
- Targeted repair. If one file was wrong, you can identify its rows and handle only those.
- Pairing with rescued data. Rows with content in
_rescued_dataare much easier to trace when you know the file.
A quick audit query looks like this:
SELECT source_file, COUNT(*) AS row_count
FROM main.bronze.orders
GROUP BY source_file
ORDER BY row_count DESC;Common errors
- Selecting
_metadatawithSELECT *and expecting it. It is hidden, so it will not appear. Reference_metadata.file_pathexplicitly. - Naming a data column
_metadata. If your source data already has a column with that name, the data column takes precedence and the hidden one is not reachable by that name. Rename the source column or select it with an explicit alias. - Forgetting it in a rewrite. If you later copy data from the bronze table to another table, the
source_filecolumn travels with it only if you select it. - Treating the path as a stable key. A file that is replaced at the same path has a new modification time. If you need to tell versions apart, keep
file_modification_timeas well asfile_path.
How it relates to the SQL version
The SQL streaming table form uses the same field. It is shown here as a reminder of how little changes:
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'
);Exercise
You run an Auto Loader stream without any withColumn call that uses _metadata. A week later you want to know which file produced a bad row in the target table. Can you recover it from the table, and what should you have done?
Show answer
No. _metadata is a hidden column that exists only while reading, so it is not written to the table unless you copy it. You should have added a column such as .withColumn("source_file", col("_metadata.file_path")) before writing. For rows already loaded, you would need to reprocess the files into a new table with the column added.
Exercise
Which two _metadata fields would you keep so you can tell apart two versions of a file that was overwritten at the same path?
Show answer
file_path and file_modification_time. The path stays the same, but the modification time changes when the file is replaced.