Sub topic 2
Reading files with the cloudFiles source
This chapter shows how to read new files from cloud storage with the cloudFiles source and write them to a Delta table. It assumes you know basic Python, Spark DataFrames, the readStream and writeStream API, and Delta tables with cloud storage paths.
What the cloudFiles source is
Auto Loader is a Structured Streaming source. You select it with format("cloudFiles") on spark.readStream. It finds files that arrived in a storage path since the last run and returns their contents as a streaming DataFrame. Because it is a normal streaming source, you write the result with writeStream like any other stream.
The source needs to know three things:
- The format of the files, set with
cloudFiles.format. - Where to store the inferred schema, set with
cloudFiles.schemaLocation. - The path to watch, passed to
load().
A minimal stream
The example below reads JSON files from a landing folder and appends them to a Delta table.
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"))Here is what each part does:
| Part | Purpose |
|---|---|
| `format("cloudFiles")` | Selects the Auto Loader source |
| `cloudFiles.format` | Names the file format: `json`, `csv`, `parquet`, and others |
| `cloudFiles.schemaLocation` | Folder where Auto Loader saves the schema it infers |
| `load(path)` | The directory to watch for new files |
| `checkpointLocation` | Folder where the stream records which files it has processed |
| `trigger(availableNow=True)` | Processes all pending files, then stops |
| `toTable(...)` | Appends the rows to a Delta table |
Keep the schema location and the checkpoint location separate, and give each stream its own pair. Sharing them between streams corrupts the saved state.
Format-specific options
Options for the file format itself go on the same reader. Spark's normal reader options still apply. For CSV, for example, you set header as you would in a batch read.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "csv")
.option("header", "true")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/events")
.option("cloudFiles.useNotifications", "true")
.option("cloudFiles.maxFilesPerTrigger", "500")
.load("s3://my-bucket/landing/events"))This example also shows where two later settings live. cloudFiles.useNotifications selects the file discovery mode, and cloudFiles.maxFilesPerTrigger caps the number of files in each micro-batch. Both use the cloudFiles. prefix. Options without that prefix, such as header, go to the underlying file format reader.
How the stream flows
Common errors
- No schema location. With a format that needs inference, such as JSON or CSV, the stream fails if
cloudFiles.schemaLocationis missing and you gave no schema. - Missing checkpoint. Without
checkpointLocation, the stream cannot track progress. Set it explicitly for any stream that writes to a table. - Reused paths. Pointing two streams at the same checkpoint or schema location causes conflicts. Use one pair per stream.
- Wrong format option. If
cloudFiles.formatdoes not match the files, rows come back empty or malformed.
Exercise
A teammate's Auto Loader stream reads CSV files but treats the header row as data. Which option do they most likely need, and does it use the cloudFiles. prefix?
Show answer
They need .option("header", "true"). It has no cloudFiles. prefix, because it is a CSV reader option passed to the underlying file format reader, not an Auto Loader option.
Exercise
Why should two different streams not share the same checkpointLocation or cloudFiles.schemaLocation?
Show answer
Each location stores state for one stream: the checkpoint records which files that stream has processed, and the schema location records its inferred schema. Sharing them mixes the state of two streams and can corrupt it or cause failures.