Sub topic 1
How Structured Streaming models a stream
Prerequisites
This chapter assumes you can do basic PySpark DataFrame work (select, filter, groupBy, join) and know what a Delta table is. You should also be able to run a notebook on a Databricks cluster and read short Python and SQL snippets. No streaming experience is needed.
The unbounded table model
Structured Streaming treats a stream as a table that never stops growing. Each new record that arrives is a new row appended to the end of that table. You write a query against this table with the same DataFrame operations you use on batch data. Spark works out how to run that query incrementally.
This is why a streaming query looks almost like a batch query. The main difference is the entry point: spark.readStream returns a streaming DataFrame, and writeStream starts the query.
from pyspark.sql import functions as F
events = spark.readStream.table("main.demo.orders_bronze")
big_orders = events.filter(F.col("amount") > 100)
query = (
big_orders.writeStream
.option("checkpointLocation", "/Volumes/main/demo/chk/big_orders")
.toTable("main.demo.big_orders")
)Nothing runs until toTable (or start) is called. Before that, events and big_orders are only descriptions of a computation.
Micro-batch execution
Databricks runs streaming queries as a series of micro-batches. A micro-batch is a small batch job over the rows that arrived since the previous one. Each cycle does the following:
- Spark asks the source which new data is available and decides the range of offsets to read. An offset is a position in the source, such as a file list or a Kafka offset.
- Spark records that range in the checkpoint before processing it.
- Spark runs the query on that slice of data as an ordinary batch job.
- Spark writes the result to the sink and records that the batch committed.
The result is the same as if you had run one big batch query over all the data so far. Spark only does the work for the new rows. Later chapters cover state and checkpoints in detail.
What the query needs to run
Every streaming query depends on four things. Each gets its own chapter:
| Piece | What it decides |
|---|---|
| Source | Where new rows come from (files, Delta, Kafka) |
| Output mode | What gets written each batch (append, update, complete) |
| Trigger | When the next batch starts |
| Checkpoint | Where progress is stored so the query can restart |
Common errors
- Forgetting the checkpoint. On Databricks, a missing checkpoint location is a common mistake. Without one, the query cannot resume from where it stopped, so give each query its own path.
- Sharing a checkpoint between queries. Two queries writing to one checkpoint path corrupt each other's progress. Use one path per query.
- Expecting batch actions to work. Calling
show()orcount()on a streaming DataFrame raises anAnalysisException. UsewriteStreaminstead. - Assuming the query ends. A query started with the default trigger runs until you stop it or it fails. In a notebook, it keeps running after the cell finishes.
Exercise
A teammate calls df.count() on a DataFrame created with spark.readStream and gets an error. Why does this fail, and how should they get the count?
Show answer
A streaming DataFrame represents an unbounded table, so a one-time action like count() has no defined end point. They should write the stream to a Delta table with writeStream and run SELECT COUNT(*) on that table, or use an aggregation inside the streaming query.
Exercise
In a micro-batch, why does Spark record the offset range in the checkpoint before it processes the data?
Show answer
The checkpoint lets a restarted query know which range of data it planned. If the query fails mid-batch, it can replay that same range instead of skipping or choosing a different one.