Sources and Sinks

Concepts covered: Structured Streaming

A streaming query has 2 ends. The source is where each micro-batch gets its new rows. The sink is where each batch's output lands. Choosing them is the first decision in any streaming design, because each end has to support the batch loop, and some systems cannot.

Rows enter from the source, pass through one batch job at a time, and land in the sink, where other systems read them.
  • Ride request events (queue, message log topic)
  • Requested-rides query (transform, Spark, micro-batches)
  • requested_rides (storage, Parquet files)
  • Zone demand dashboard (consumer, reads the files)
  • Ride request events -> Requested-rides query
  • Requested-rides query -> requested_rides
  • requested_rides -> Zone demand dashboard
  • Source: Ride request events
  • Query: Requested-rides query
  • Sink: requested_rides
  • Readers: Zone demand dashboard

Built-in sources

Spark ships with a short list of built-in sources. You pick one by name with format(...), the same call a batch read uses.

SourceA new row isPosition Spark tracksUsed for
Files in a directory (parquet, json, csv, text)A row in a file that appeared since the last batchWhich files it has already readProduction
Message logA message appended to a topicAn offset per topic partitionProduction
rateA generated row with a timestamp and a counter valueThe counterTesting
socketA line of text from a network connectionNone that can be rereadTesting only

A message log is a service that stores events in arrival order and lets many readers fetch them later. Events are grouped into named topics, each topic is split into partitions, and every message in a partition gets a sequence number called an offset. It was built to hold a stream of events and hand out any range of them on request.

What a source must provide

3 requirements

BatchOffsets it coversRows
64,169 to 4,17911
74,180 to 4,1889
84,189 to 4,20012

Batch 8 starts at offset 4,189 because batch 7 ended at 4,188, so no row falls between batches and no row lands in 2 of them. A socket source fails the second requirement: once a line is read from the connection it is gone, so a failed batch loses its rows, and Spark's documentation marks the source for testing only. The third requirement comes from what inference costs: a batch read that infers a schema makes an extra pass over the files before the real read, and a stream has no finished files to make that pass over.

Built-in sinks

SinkWhat each batch doesUsed for
Files (parquet, json, csv)Adds new files to an output directoryProduction tables
Message logAppends messages to a topicFeeding other streams and services
consolePrints the batch's rows on the driverDebugging
memoryAdds the rows to a table held in driver memoryDebugging

What a sink must provide mirrors the source: it has to accept output again and again, batch after batch, adding to what is already there. A file sink does this by writing new files for each batch and leaving earlier files alone. A sink that can only replace its whole contents on every write doesn't fit the loop.

The 2 debugging sinks carry a warning you have heard before. console and memory both bring every output row to the driver, which is the collect() trap on a timer. They suit 20 rows on a laptop and nothing larger.

A team lands a new JSON file of ride events in a storage directory every minute and wants the cleaned rows kept as a table that analysts query. Which pair of ends fits?

Both production ends are places you already read and write in batch jobs: directories of files, and tables built from them. A stream uses the same storage. What differs is how the read and the write are declared, which comes down to 2 method names and a few options.

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.