Sub topic 5
Choosing an output mode
This chapter assumes you know basic PySpark DataFrame operations and have read how Structured Streaming treats a stream as an unbounded table. Here you will learn what each output mode writes and how to pick one.
What an output mode controls
Each micro-batch updates a logical result table. The output mode decides which rows of that result table go to the sink after the batch finishes. You set it with outputMode(...) on writeStream.
| Mode | Rows written per batch | Typical use |
|---|---|---|
| append | Only new rows that will never change | Ingestion, filters, watermarked windows |
| update | Only rows that changed in this batch | Running aggregates to a sink that can overwrite |
| complete | The entire result table | Small aggregates, such as a dashboard table |
Append mode
Append is the default. Spark emits a row once and promises not to revise it. Queries with only select, filter and map work in append mode, because each input row produces its output row immediately.
An aggregation can use append mode only when Spark knows a group is final. That requires a watermark on the event-time column. Spark holds each window back until the watermark passes its end, then emits it once.
from pyspark.sql import functions as F
events = spark.readStream.table("main.demo.orders_bronze")
counts = (
events.withWatermark("event_time", "10 minutes")
.groupBy(F.window("event_time", "5 minutes"), "customer_id")
.agg(F.count("*").alias("orders"))
)
(
counts.writeStream
.outputMode("append")
.option("checkpointLocation", "/Volumes/main/demo/chk/orders_counts")
.toTable("main.demo.orders_5min")
)The trade-off is latency. A window ending at 10:05 with a 10-minute watermark appears only after Spark sees events later than 10:15.
Update mode
Update writes only the rows whose values changed in the current batch. You see results sooner, because a partial count appears before the window closes. The sink must handle repeated versions of the same key. A plain append-only Delta table would collect duplicates, so pair update mode with an upsert such as foreachBatch and MERGE. Without an aggregation, update mode behaves like append mode.
Complete mode
Complete rewrites the whole result table each batch. Spark must keep every group in state, and a watermark cannot drop old state. Use it only when the number of groups is small and bounded, such as counts per country.
by_country = (
spark.readStream.table("main.demo.orders_bronze")
.groupBy("country")
.count()
)
(
by_country.writeStream
.outputMode("complete")
.option("checkpointLocation", "/Volumes/main/demo/chk/by_country")
.toTable("main.demo.orders_by_country")
)Choosing a mode
Common errors
- Aggregation in append mode without a watermark. Spark raises an
AnalysisExceptionsaying append mode needs a watermark on the aggregation. AddwithWatermarkor change the mode. - Watermark on the wrong column. The watermark column must be the same one used in the window, and it must be set before the
groupBy. - Complete mode on a growing key set. State grows without limit and the query eventually runs out of memory.
- Update mode into an append-only table. The table accumulates several versions of each key.
Exercise
A query counts orders per customer in 5-minute windows. Downstream users need each window's count exactly once, and a few minutes of delay is acceptable. Which output mode and which extra setting do you use?
Show answer
Use append mode with a watermark on the event-time column, for example withWatermark("event_time", "10 minutes"). Spark emits each window once, after the watermark passes the end of the window, and it can drop old state.
Exercise
Why is complete mode a poor choice for counting orders per customer_id when you have millions of customers?
Show answer
Complete mode keeps every group in state and rewrites the whole result table every batch. With millions of keys, state and write cost grow without bound, and a watermark cannot remove old state.