The full answer
A DStream (discretised stream) is the abstraction of Spark's original streaming API: a continuous stream represented as a sequence of RDDs, one per batch interval. Spark Streaming collected incoming data for, say, 5 seconds, turned it into an RDD, ran RDD operations on it, and repeated. DStreams are now legacy: Spark's documentation says Spark Streaming is the previous generation of the engine, receives no more updates, and that new applications should use Structured Streaming.
How DStreams worked
from pyspark.streaming import StreamingContext
ssc = StreamingContext(spark.sparkContext, batchDuration=5) # 5-second batches
lines = ssc.socketTextStream("localhost", 9999)
counts = (lines.flatMap(lambda line: line.split(" "))
.map(lambda w: (w, 1))
.reduceByKey(lambda a, b: a + b))
counts.pprint()
ssc.start()
ssc.awaitTermination()
- Input DStreams came from sources such as Kafka, Kinesis, sockets and files, many through receivers that ran as long-lived tasks on executors.
- Transformations mirrored RDD operations (
map,filter,reduceByKey,join), applied to each batch's RDD. - Window operations (
window,reduceByKeyAndWindow) combined several batches, such as a 1-minute window sliding every 10 seconds. - Stateful operations (
updateStateByKey,mapWithState) carried state across batches. - Output operations (
foreachRDD,saveAsTextFiles) wrote results. - Checkpointing stored metadata and state for recovery; write-ahead logs protected receiver data.
Their role, and their limits DStreams brought micro-batch streaming to Spark and unified it with batch processing on RDDs. But they:
- worked on processing time, not event time, so late or out-of-order data was hard to handle correctly;
- used RDDs, so no Catalyst optimisation;
- left exactly-once output to the developer;
- had a separate API from batch DataFrame code.
What replaced them
Structured Streaming treats a stream as an unbounded table and uses the DataFrame and SQL API, with event-time windows, watermarks, built-in exactly-once guarantees with supported sinks, and optimisation. New integrations, such as the Kafka connector improvements and the newer stateful API transformWithState, are built only for Structured Streaming.
In an interview
Explain what DStreams are, then say you would build new streaming jobs with Structured Streaming and migrate existing DStream jobs: map the batch interval to a trigger interval, updateStateByKey to transformWithState or a watermarked aggregation, and foreachRDD to foreachBatch.
Last updated 16 October 2026
Sources