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.
- 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.
| Source | A new row is | Position Spark tracks | Used for |
|---|---|---|---|
| Files in a directory (parquet, json, csv, text) | A row in a file that appeared since the last batch | Which files it has already read | Production |
| Message log | A message appended to a topic | An offset per topic partition | Production |
| rate | A generated row with a timestamp and a counter value | The counter | Testing |
| socket | A line of text from a network connection | None that can be reread | Testing 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
- A position. Every row needs an address, such as an offset or a file name, so the driver can define a batch as a range: offsets 4,180 to 4,188.
- A second read of the same range. If a batch's job fails partway, Spark runs it again over the identical range, so the source has to return those rows a second time.
- A schema known before the data. A batch read can infer a schema by scanning the files. A file stream has to be told its schema, because the files it will read do not exist yet.
| Batch | Offsets it covers | Rows |
|---|---|---|
| 6 | 4,169 to 4,179 | 11 |
| 7 | 4,180 to 4,188 | 9 |
| 8 | 4,189 to 4,200 | 12 |
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
| Sink | What each batch does | Used for |
|---|---|---|
| Files (parquet, json, csv) | Adds new files to an output directory | Production tables |
| Message log | Appends messages to a topic | Feeding other streams and services |
| console | Prints the batch's rows on the driver | Debugging |
| memory | Adds the rows to a table held in driver memory | Debugging |
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?
- A socket source and a console sink, since neither needs any setup.
- A rate source and a file sink, since rate tracks its own counter.
- A file source on the directory and a memory sink that analysts query.
- A file source on the directory and a file sink writing Parquet.
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.