Sub topic 4
Using Delta tables as sources and sinks
This chapter assumes you know basic PySpark DataFrame operations, Delta Lake tables, and Unity Catalog tables and volumes. It also assumes you have read the earlier chapters on the streaming model and on reading from streaming sources.
Why Delta fits streaming
A Delta table can be both the input and the output of a streaming query. As a sink, Delta commits each micro-batch as one atomic transaction, so readers never see half a batch. As a source, Delta reads its own transaction log, so a stream sees each committed change once, in order. This is why a common design chains tables: a bronze table feeds a silver table, which feeds a gold table, with each hop a streaming query.
Reading a Delta table as a stream
Use readStream.table() with the table name. The query first processes the data already in the table, then picks up new commits.
events = spark.readStream.table("main.demo.orders_bronze")The checkpoint records how far the query has read in the source's log. On restart, the query continues from that point.
Writing a stream to a Delta table
Use writeStream with a checkpoint location and toTable().
query = (
events.filter("amount > 0")
.writeStream
.option("checkpointLocation", "/Volumes/main/demo/chk/orders_silver")
.toTable("main.demo.orders_silver")
)The default output mode for Delta is append. Each checkpoint location must belong to exactly one query. Do not share one between two queries.
Source tables must be append-only
A streaming read expects the source to only grow. If someone runs UPDATE, DELETE or MERGE on the source, the files it rewrites look like changes the stream cannot treat as plain appends. The query then fails with an error about updates or deletes in the source.
You have three options:
- Keep the source append-only and put updates in a different table.
- Set
skipChangeCommitstotrueto ignore commits that update or delete rows. The stream then does not see those changes. - Read the change data feed to get row-level inserts, updates and deletes.
events = (
spark.readStream
.option("skipChangeCommits", "true")
.table("main.demo.orders_bronze")
)changes = (
spark.readStream
.option("readChangeFeed", "true")
.table("main.demo.customers")
)The change data feed must be enabled on the table first, with the table property delta.enableChangeDataFeed = true. The output then has extra columns such as _change_type and _commit_version.
Controlling batch size
By default a Delta source can read a very large batch on the first run. Limit it with maxFilesPerTrigger or maxBytesPerTrigger. These are soft limits for bytes and a hard limit for files.
events = (
spark.readStream
.option("maxFilesPerTrigger", 100)
.table("main.demo.orders_bronze")
)Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Failure about updates or deletes in the source | Source table was modified, not only appended to | Use `skipChangeCommits` or the change data feed |
| A restart reprocesses old data | Checkpoint was deleted or points to a new path | Keep the checkpoint with the target table |
| Two queries fail or corrupt progress | They share one checkpoint location | Give each query its own checkpoint |
| Huge first batch | No rate limit on the initial backlog | Set `maxFilesPerTrigger` or `maxBytesPerTrigger` |
Exercise
A streaming query reads main.demo.customers. A nightly job runs MERGE INTO on that table, and the stream starts failing. Name two ways to fix this and what each one costs you.
Show answer
Set skipChangeCommits to true, which keeps the stream running but means it never sees the updated or deleted rows. Or enable the change data feed and read it with readChangeFeed, which delivers the changes but requires you to handle the _change_type column downstream.