A Stream Is a Growing Table

Concepts covered: Structured Streaming

Every Spark job you have run so far read data that was already finished. The files sat in storage, the job read all of them, an action returned an answer, and the job ended. Ride requests work differently: a new one arrives every few milliseconds, and there is no moment when the data is complete.

Stream processing means computing over data while it's still arriving. Spark's version is called Structured Streaming, and it rests on one idea that lets you reuse everything you know about DataFrames: treat the stream as a table.

A table that only gains rows

Picture a table named ride_requests. Each time a rider requests or cancels, one row is appended to the bottom. Existing rows never change and never leave. Spark calls this the input table, and because rows keep arriving it has no last row. Suppose it starts empty at 12:00:00. Here are the 7 rows that arrive in the next 20 seconds, with the second each one landed.

ride_idzonestatusarrived_at
101SoMarequested12:00:02
102Missionrequested12:00:05
103SoMacancelled12:00:07
104SoMarequested12:00:09
105Marinarequested12:00:12
106SoMarequested12:00:15
107Missioncancelled12:00:18

At 12:00:10 the table holds rides 101 to 104. At 12:00:20 it holds all 7. Ask how many rows the table has, and the answer depends on when you ask.

A query that stays standing

A batch query is asked once and answered once. A streaming query is defined once and stays in place while the table grows, which is why it is called a standing query. You write it with the same DataFrame operations you already use.

requested_by_zone = (
    ride_requests
    .filter(F.col("status") == "requested")
    .groupBy("zone")
    .count()
)

A standing query writes its output to a second table, called the result table, which Spark keeps up to date. The rule that defines it fits in one sentence: at any moment, the result table holds what the same query would return if you ran it as a batch job over every row that has arrived so far.

Check the right-hand table against the 7 input rows. Rides 103 and 107 are cancelled, so the filter drops them. That leaves 3 requested rides in SoMa, plus 1 each in Mission and Marina. Between 12:00:10 and 12:00:20 nobody reran anything, and the result still took in rides 105 to 107 as they arrived.

Batch query
  • Input is a finished table
  • Runs when you call an action
  • Returns one answer, then ends
  • A rerun reads every row again
Standing query
  • Input is a table that keeps gaining rows
  • Runs from the moment it is started
  • Keeps a result table current
  • Ends when it's stopped or fails

One API for batch and streaming

Before this model, streaming code in Spark was written against a separate API with its own operations, so one piece of logic often lived in 2 codebases: a batch job for history and a streaming job for live data. Treating the stream as a table removes the split. The filter and the groupBy above are the DataFrame calls you know, Catalyst optimizes them the same way, and the plan contains the same shuffle on zone that a batch job would.

The model also gives you a self-check. When a streaming result puzzles you, freeze the input at one instant, run the query as a batch job over those rows in your head, and compare. The result table at that instant has to match.

At 12:00:25 one more row arrives in ride_requests: ride 108, Marina, cancelled. What does the result table hold once Spark has taken that row into account?

Hold on to the 2 tables. The input table grows by appends, the result table follows it, and the query between them is ordinary DataFrame code. What remains is how Spark keeps the result current without rereading rides 101 to 107 every time ride 108 shows up.

About this lesson

This section is part of the Structured Streaming: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.