Structured Streaming: Output Modes, Sinks, Triggers
Databricks Spark Developer Associate · Lesson 15 of 18 · 2:00:31
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Associate Developer for Apache Spark exam. In this lesson: the Structured Streaming model, readStream and writeStream, checkpoints, the append, complete, and update output modes, sinks, triggers, and exactly-once guarantees. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
A live leaderboard
Coralpeak wants a live leaderboard: purchases arrive every second, and the scores must change within seconds, without losing a purchase or counting one twice, even after a crash. A batch job every night is no longer enough. This lesson covers the streaming model, reading and writing streams, output modes, and sinks and triggers.
The streaming model
Structured Streaming treats a stream as an unbounded input table, where every new record is a new row appended to the table. You write the query exactly as in batch, with the same DataFrame API, and Spark runs it incrementally, updating a result table at each trigger. By default, the engine processes the data in micro-batches, with latencies down to about one hundred milliseconds, and exactly once guarantees. Exactly once, from end to end, relies on sources that can replay data from tracked offsets, a checkpoint with write-ahead logs, and sinks that can safely write the same batch again. A continuous processing mode lowers the latency to about a millisecond, but it only gives at least once guarantees, and supports few operations.
Reading and writing streams
spark.readStream returns a streaming DataFrame from a source, such as files arriving in a folder, Kafka, or a Delta table, and file sources need a schema up front. writeStream defines the output: the format or the table, the output mode, the trigger, and the checkpoint location. start launches the query in the background, and returns a handle to the streaming query, while awaitTermination blocks until the query stops. The checkpoint location stores the offsets and the state, so a restarted query resumes exactly where it stopped, and each query needs its own location.
Output modes
The output mode decides which rows of the result table reach the sink at each trigger. Append, the default, writes only the new rows, and every row is written once and never changed, which suits queries without aggregation. Complete rewrites the whole result table at each trigger, which only works with aggregations, since Spark must keep the full result. Update writes only the rows that changed since the last trigger, such as the scores that moved. An aggregation in append mode needs a watermark on event time, so that Spark knows when a window is final, and can write it once.
Sinks and triggers
File sinks write in append mode, with exactly once guarantees, while the Kafka sink writes to a topic, with at least once guarantees. foreachBatch hands each micro-batch to your own function as a regular DataFrame, so that you can use batch writers, such as a merge or a JDBC write, while foreach processes each row. The console and memory sinks are only for debugging, since they print or keep the data on the driver. The trigger decides when micro-batches run: by default, as soon as the previous one finishes, or at a fixed processing time interval, such as every minute. availableNow processes all the data available at the start, in several batches, then stops, which suits jobs scheduled like batches, and replaces the older once trigger.
Recap
Let's recap. A stream is an unbounded input table, queried as in batch, with a result table updated at each trigger. Micro-batches give exactly once guarantees, through replayable sources, the checkpoint, and sinks that can safely write a batch again. readStream and writeStream define the input and the output, and each query needs its own checkpoint location. Append writes new rows only, complete rewrites the whole result, and update writes the changed rows. And foreachBatch hands each micro-batch to your code, while availableNow processes what is there, then stops.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
A streaming query restarts after a cluster failure, and processes every file of the landing folder again, from the start. What was most likely missing?
- A schema on the reader
- The append output mode
- A call to awaitTermination()
- A checkpoint location for the query
Show the answer
A checkpoint location for the query
Without a checkpoint, the restarted query has no record of the offsets it already processed. “A schema on the reader” is wrong, since a missing schema would stop the read, not repeat it. “The append output mode” is the default mode, and has nothing to do with progress. And “A call to awaitTermination()” only blocks the program until the query stops.
A query keeps a running total of points for each player, and at each trigger, the sink must receive the totals of all the players, not only those that changed. Which output mode fits?
- append
- complete
- update
- errorifexists
Show the answer
complete
Complete writes the whole result table at each trigger, so the total of every player is there. “append” is wrong, since append never changes a row once written, and cannot output a running aggregation without a watermark. “update” would only write the totals that changed since the last trigger. And “errorifexists” is a save mode for batch writes, not an output mode.
A team wants to run a streaming query once an hour, from a scheduler, to process everything that arrived since the last run, then stop. Which trigger fits?
- The default trigger
- processingTime="1 hour"
- availableNow=True
- continuous="1 hour"
Show the answer
availableNow=True
AvailableNow processes all the data available at the start, then stops, and the checkpoint remembers where to resume. “The default trigger” would keep the query running, starting each micro-batch as soon as the previous one ends. “processingTime="1 hour"” would also keep the query running, and the cluster with it, between the batches. And “continuous="1 hour"” is the low latency continuous mode, which never stops on its own.