Sub topic 6
Processing only new data: streaming, triggers, and checkpoints
This chapter assumes you know basic PySpark DataFrames and have read the earlier chapters on Delta tables and the bronze layer. It explains how a medallion pipeline reads only the data that arrived since the last run, and what can go wrong.
The working model
Structured Streaming treats a source as a table that keeps growing. Each run reads the rows that were added since the previous run, processes them, and writes the result. Three parts make this work:
- Source: where new data comes from. Auto Loader tracks new files. A Delta table tracks new commits.
- Trigger: how often a run happens, and how much it reads.
- Checkpoint: a folder that records what the query has already processed.
The checkpoint is what makes a run "incremental." It stores source offsets and query state. When the query restarts, it reads the checkpoint and continues from there. Without it, the query would have no memory of earlier runs.
Choosing a trigger
The trigger decides whether the query runs continuously or as a scheduled batch.
| Trigger | Behavior | Typical use |
|---|---|---|
| `availableNow=True` | Processes everything new, then stops | Scheduled jobs in a medallion flow |
| `processingTime="1 minute"` | Runs a micro-batch every interval, and keeps running | Low-latency pipelines |
| Default (none) | Starts the next micro-batch as soon as the last ends | Continuous streaming |
Most medallion pipelines start with availableNow=True. You get incremental processing and a cluster that shuts down after each run. You can switch to a continuous trigger later without changing the transformation logic.
A bronze-to-silver stream
Here is a stream that reads new rows from the bronze table and appends them to a staging table. The checkpoint path is unique to this query.
from pyspark.sql import functions as F
typed = (
spark.readStream.table("main.bronze.orders")
.where("order_id IS NOT NULL")
.select(
"order_id",
"customer_id",
F.col("order_ts").cast("timestamp").alias("order_ts"),
F.col("amount").cast("decimal(12,2)").alias("amount"),
"status",
)
)
query = (
typed.writeStream
.option("checkpointLocation", "/Volumes/main/sales/checkpoints/orders_silver_stage")
.trigger(availableNow=True)
.toTable("main.silver.orders_stage")
)
query.awaitTermination()Run it twice. The first run processes every bronze row. The second run finds nothing new and finishes quickly. If ten new rows land in bronze, the third run processes only those ten.
The awaitTermination() call matters in jobs. It makes the notebook wait until the run finishes, so a downstream task does not start on a half-written table.
Common errors
- Sharing a checkpoint between queries. Each query needs its own checkpoint folder. Two queries on one folder corrupt each other's progress.
- Deleting the checkpoint to "fix" a problem. The query then reprocesses the whole source from the start. In an append-only target, this creates duplicates.
- Changing the query in incompatible ways. Changing the source or adding a stateful operation can make an old checkpoint unusable. Use a new checkpoint location and plan a backfill.
- Streaming from a table that gets updates or deletes. A stream expects append-only input. If an upstream job rewrites rows, the stream fails unless you handle changes explicitly.
- Forgetting that the checkpoint tracks the source, not the target. If you drop the target table but keep the checkpoint, the query will not reload old data.
Exercise
A silver stream runs with availableNow=True every hour. Someone deletes its checkpoint folder to "start clean," but leaves the target table in place. What happens on the next run, and how would you avoid the problem?
Show answer
The query has no record of earlier progress, so it reads the whole bronze table again and appends every row to the existing silver table, which creates duplicates. To avoid it, either drop and rebuild the target table together with the checkpoint, or make the write idempotent with MERGE (covered in the deduplication chapter) so that reprocessed rows update existing rows instead of duplicating them.