Spark Streaming (DStreams)
| Also known as | Spark Streaming, DStream API, discretized streams |
|---|---|
| Field | Stream processing |
| Introduced | Apache Spark 0.7.0 (alpha) |
| Key parts | StreamingContext, DStream, receivers, batch interval, checkpointing
|
| Open source implementations | Apache Spark (streaming module)
|
| Related | Structured Streaming, Stream processing, Apache Spark |
Spark Streaming, also called the DStream API, is the original stream processing library of Apache Spark, in which a live data stream is divided into small batches and represented as a discretized stream (DStream): a continuous series of RDDs, each holding the data received in one time interval.[3] The API has been deprecated since Spark 3.4.0. Spark's documentation calls it the previous generation of Spark's streaming engine and directs all new streaming applications to Structured Streaming.[3][1]
Status
The DStream API was deprecated in Apache Spark 3.4.0 under SPARK-42075, "Deprecate DStream API", and the 3.4.0 release notes list the deprecation.[1][2] At the v4.3.0 tag the deprecation is still in place:
- The Scala
StreamingContextand the JavaJavaStreamingContextcarry@deprecated("DStream is deprecated. Migrate to Structured Streaming.", "Spark 3.4.0").[4] - The PySpark
StreamingContextconstructor issues aFutureWarning: "DStream is deprecated as of Spark 3.4.0. Migrate to Structured Streaming." Its docstring adds that there are no longer updates to DStream and that it is a legacy project.[5] - The Spark 4.3.0 programming guide opens with a note that Spark Streaming "is the previous generation of Spark's streaming engine", that "there are no longer updates to Spark Streaming and it's a legacy project", and that users should use Structured Streaming.[3]
The API has not been removed. The streaming module and its programming guide are still part of Spark 4.3.0, and the guide still documents the Kafka and Kinesis integrations.[3] No Spark release up to 4.3.0 announces a removal date.
Programming model
A Spark Streaming application creates a StreamingContext with a batch interval, such as one second. Input DStreams receive data from a source; every input DStream except file streams is associated with a receiver, which receives the data and stores it in Spark's memory for processing. Basic sources include sockets and file systems, and advanced sources such as Kafka and Kinesis come from separate artifacts (spark-streaming-kafka-0-10 and spark-streaming-kinesis-asl).[3]
Each operation on a DStream becomes an operation on the RDDs underneath it. The API offers RDD-style transformations (map, flatMap, reduceByKey), window operations such as reduceByKeyAndWindow, stateful updateStateByKey, transform for arbitrary RDD-to-RDD functions, and output operations such as foreachRDD. Stateful transformations, such as updateStateByKey or reduceByKeyAndWindow with an inverse function, require a checkpoint directory.[3] The window length and sliding interval of a window operation must be multiples of the batch interval.[3]
The following PySpark example, adapted from the programming guide, counts words in one-second batches from a socket. On Spark 4.3 it runs but prints the deprecation warning.[3][5]
from pyspark import SparkContext
from pyspark.streaming import StreamingContext
sc = SparkContext("local[2]", "NetworkWordCount")
ssc = StreamingContext(sc, 1) # 1-second batches; emits a FutureWarning
lines = ssc.socketTextStream("localhost", 9999)
counts = (lines.flatMap(lambda line: line.split(" "))
.map(lambda word: (word, 1))
.reduceByKey(lambda a, b: a + b))
counts.pprint()
ssc.start()
ssc.awaitTermination()
DStreams are built on RDDs and the SparkContext, which the Spark Connect documentation lists as unsupported, so DStream applications cannot run through a Spark Connect client.[6]
History
Spark Streaming first appeared as an alpha in Spark 0.7.0, which described it as an extension adding near-real-time processing, with transformations such as map, filter, reduce and sliding windows.[7] Structured Streaming, built on the Spark SQL engine, arrived as an experimental API in Spark 2.0.0.[8] The Spark 4.3 documentation recommends it for all new streaming applications and pipelines.[3]
Migration to Structured Streaming
There is no automatic converter. Migration means rewriting the job against the DataFrame API. The table maps DStream concepts to their usual Structured Streaming counterparts in Spark 4.3.[3][9]
| DStream API | Structured Streaming |
|---|---|
StreamingContext with a batch interval |
SparkSession; spark.readStream to read, writeStream with a processing-time trigger to set the micro-batch interval
|
| DStream (series of RDDs) | Streaming DataFrame or Dataset (an unbounded table) |
RDD transformations (map, reduceByKey) |
DataFrame operations (select, groupBy, agg) or SQL
|
window, reduceByKeyAndWindow (multiples of the batch interval) |
groupBy(window(...)) on an event-time column, with a watermark for late data
|
updateStateByKey, mapWithState (Scala and Java)[10] |
Built-in stateful aggregations, or transformWithState (Spark 4.0 and later) for arbitrary state[11]
|
foreachRDD |
foreachBatch or a built-in sink
|
ssc.checkpoint(dir) |
checkpointLocation option on each query
|
spark-streaming-kafka-0-10 |
Kafka source and sink in spark-sql-kafka-0-10[12]
|
The word count above becomes the following query, taken from the Structured Streaming quick example.[13] One semantic difference matters: the DStream version prints counts for each batch, while this query keeps a running count over all data received, because the result table is cumulative and is written in complete mode.
from pyspark.sql import SparkSession
from pyspark.sql.functions import explode, split
spark = SparkSession.builder.appName("StructuredNetworkWordCount").getOrCreate()
lines = (spark.readStream.format("socket")
.option("host", "localhost").option("port", 9999).load())
words = lines.select(explode(split(lines.value, " ")).alias("word"))
counts = words.groupBy("word").count()
query = counts.writeStream.outputMode("complete").format("console").start()
query.awaitTermination()
Unlike DStreams, Structured Streaming queries run on the optimized Spark SQL engine and provide end-to-end exactly-once semantics through checkpointing and write-ahead logs.[14] Most of the Structured Streaming API, including DataStreamReader, DataStreamWriter and StreamingQuery, is supported in Spark Connect.[6]
See also
References
- ↑ 1.0 1.1 1.2 SPARK-42075: Deprecate DStream API. Apache Software Foundation JIRA.Retrieved 2026-11-22.
- ↑ 2.0 2.1 Spark Release 3.4.0. Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 3.00 3.01 3.02 3.03 3.04 3.05 3.06 3.07 3.08 3.09 Spark Streaming Programming Guide (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ StreamingContext.scala at v4.3.0. GitHub.Retrieved 2026-11-22.
- ↑ 5.0 5.1 pyspark/streaming/context.py at v4.3.0. GitHub.Retrieved 2026-11-22.
- ↑ 6.0 6.1 Spark Connect Overview (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Spark Release 0.7.0. Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Spark Release 2.0.0. Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Structured Streaming: APIs on DataFrames and Datasets (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ PairDStreamFunctions.scala at v4.3.0. GitHub.Retrieved 2026-11-22.
- ↑ Structured Streaming: transformWithState (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Structured Streaming + Kafka Integration Guide (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Structured Streaming: Getting Started (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Structured Streaming Programming Guide (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.