Sub topic 5
Schema inference and the schema location
This chapter assumes you can write a basic Structured Streaming query with readStream and writeStream. It also assumes you know what a Delta table is and have seen the cloudFiles source from the earlier chapters.
Why Auto Loader infers a schema
Files such as JSON and CSV carry no schema of their own. Auto Loader can infer one for you, so you don't have to write a StructType by hand. It does this once, when the stream first starts, and then stores the result so later runs stay consistent.
What the schema location does
When you ask Auto Loader to infer a schema, you must set cloudFiles.schemaLocation. This is a directory where Auto Loader saves the inferred schema. Without it, a stream that has no explicit schema fails at startup.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.load("/Volumes/main/raw/landing/orders"))
(df.writeStream
.option("checkpointLocation", "/Volumes/main/raw/_checkpoints/orders")
.trigger(availableNow=True)
.toTable("main.bronze.orders"))Auto Loader writes the schema under a _schemas folder inside the schema location. On the first run it infers the schema and saves it. On later runs it reads the saved schema and skips inference.
How inference samples your data
Auto Loader does not read every file to infer the schema. It samples the first 50 GB of data or the first 1,000 files, whichever limit is reached first. A column that appears only in later files will not be in the first schema. Later chapters cover how schema evolution handles this.
Column types
The file format decides what you get:
| Format | Types without extra options | With `cloudFiles.inferColumnTypes` |
|---|---|---|
| JSON, CSV | All columns are `STRING` | Types such as `BIGINT` and `TIMESTAMP` are inferred |
| Parquet, Avro | Types come from the file | No change |
Strings are the default for JSON and CSV because a sample can mislead. A column that looks numeric in the sample may hold text in a later file. Set cloudFiles.inferColumnTypes to true when you accept that risk:
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.option("cloudFiles.inferColumnTypes", "true")
.load("/Volumes/main/raw/landing/orders"))Auto Loader also adds a _rescued_data column when it infers a schema. It holds values that don't fit the schema. The next chapter covers it.
Common errors
- Missing
schemaLocation. The stream fails at startup because there is nowhere to store the inferred schema. Set the option, or supply a schema with.schema(...). - Sharing one schema location between streams. Each stream needs its own directory. Two streams writing schema versions to the same place will overwrite or confuse each other.
- Deleting the schema location but keeping the checkpoint. The stream re-infers a schema that may differ from the one the checkpoint's earlier batches used. Treat the two directories as a pair. Reset both together, or neither.
- Expecting numbers from CSV or JSON by default. If a downstream query fails on a
STRINGcolumn,inferColumnTypesis off. - Using a path the cluster can't write to. Inference needs write access to the schema location.
Exercise
A stream reads JSON files with cloudFiles.schemaLocation set and no other schema options. A downstream query fails because order_id is a STRING. What option fixes this, and why was the column a string?
Show answer
Set cloudFiles.inferColumnTypes to true. For JSON and CSV, Auto Loader infers every column as STRING by default, because a sample of files may not represent all later data.