Sub topic 6
Keeping unexpected data in _rescued_data
This chapter assumes you can write a basic Auto Loader stream, know that cloudFiles.schemaLocation stores the inferred schema, and are comfortable with Delta tables. Here you learn what the rescued data column holds, when rows land in it, and how to use it.
What the column is
Auto Loader adds a column named _rescued_data to the stream when it infers a schema. It holds data that did not fit the schema, so the stream does not drop it or fail. The column is a JSON string. It contains the values that could not be parsed into the schema, plus the path of the source file in a _file_path field.
Data is rescued in these cases:
- A column is in the file but not in the schema.
- A column's type does not match, for example the text
"abc"in a column typed as an integer. - A column name differs from the schema only by letter case.
For a row with no problems, _rescued_data is NULL.
The diagram below shows how a record is routed.
Seeing it in action
Suppose the schema was inferred from early files with order_id and amount. A later file arrives with an extra field coupon and a bad amount.
{"order_id": 1, "amount": 19.99}
{"order_id": 2, "amount": "n/a", "coupon": "SPRING"}With cloudFiles.inferColumnTypes set to true, amount is a double. The second row is stored like this:
| order_id | amount | _rescued_data |
|---|---|---|
| 1 | 19.99 | NULL |
| 2 | NULL | {"amount":"n/a","coupon":"SPRING","_file_path":"/Volumes/main/raw/landing/orders/part-2.json"} |
The row is kept. The values that did not fit are preserved in the JSON string.
Querying rescued data
Filter on the column to find problem rows, and parse the JSON to read a rescued field.
from pyspark.sql.functions import col, get_json_object
bad = (spark.read.table("main.bronze.orders")
.where(col("_rescued_data").isNotNull())
.select(
"order_id",
get_json_object("_rescued_data", "$.coupon").alias("coupon"),
get_json_object("_rescued_data", "$._file_path").alias("file")))
bad.show(truncate=False)Run this as a routine data quality check. A sudden rise in rows with a non-null _rescued_data usually means an upstream producer changed its format.
Interaction with schema evolution
The schema evolution mode decides whether new columns end up in _rescued_data:
| Mode | New column in the data |
|---|---|
| `addNewColumns` (default without a user-provided schema) | The stream fails once, updates the schema in the schema location, and the column appears after a restart. |
| `rescue` | The schema never changes. New columns go to `_rescued_data`. |
| `failOnNewColumns` | The stream fails until you update the schema. |
| `none` | The schema is ignored and new columns go to `_rescued_data`. |
With rescue, nothing is lost and the stream keeps running, but you must read the JSON to use the new fields.
Common errors
- Assuming a bad value fails the stream. A type mismatch produces a
NULLin the column and the original value in_rescued_data. Check the column, or you can miss bad data. - Dropping the column. If you
selectonly named columns, you lose the rescued data. Keep it in bronze tables. - Renaming the column. You can change its name with the
rescuedDataColumnoption. Use the same name in downstream queries.
Exercise
An Auto Loader stream uses cloudFiles.schemaEvolutionMode set to rescue. A new file contains a column region that is not in the schema. What happens to the stream and to the region values?
Show answer
The stream keeps running and the schema does not change. The region values are written into the _rescued_data JSON string of each affected row, together with the _file_path. You can read them with get_json_object.