Structured Streaming
| Developer | Apache Software Foundation (Apache Spark project) |
|---|---|
| Initial release | Spark 2.0.0 (2016-07-26, experimental) |
| Latest release | Spark 4.3.0 (2026-10-30) |
| Written in | Scala, Java, Python |
| Type | Stream processing engine |
| License | Apache License 2.0 |
| Website | Programming guide |
| Repository | github.com/apache/spark |
| Related | Spark Declarative Pipelines, Apache Spark |
Structured Streaming is the stream processing engine of Apache Spark, built on the Spark SQL engine, in which a streaming computation is written with the same DataFrame and Dataset operations as a batch computation on static data.[1] Spark runs the query incrementally as new data arrives and keeps the result up to date. Through checkpointing and write-ahead logs, Structured Streaming provides end-to-end exactly-once guarantees with sources and sinks that support them.[1]
Programming model
Structured Streaming treats a live data stream as a table that grows as new rows arrive. A streaming query is a query on that input table, and its output is a result table that Spark updates each time it processes new data.[2]
The output mode says what part of the result table is written to the sink after each update:[2]
- Append: only rows added since the last trigger. It applies to queries whose existing result rows never change.
- Complete: the entire result table.
- Update: only rows that changed since the last trigger (available since Spark 2.1.1). Without aggregations it behaves like append mode.
Streaming queries can aggregate over event-time windows, including tumbling, sliding and session windows. A watermark tells Spark how late data may arrive, so that it can finalize old windows and drop their state. Structured Streaming also supports stream-static joins, stream-stream joins and deduplication.[3]
The following PySpark example counts rows from the built-in rate test source in 30-second event-time windows and prints finished windows to the console. It runs on Spark 4.3.
from pyspark.sql import SparkSession
from pyspark.sql.functions import window
spark = SparkSession.builder.appName("rate-counts").getOrCreate()
events = spark.readStream.format("rate").option("rowsPerSecond", 10).load()
counts = (
events.withWatermark("timestamp", "1 minute")
.groupBy(window("timestamp", "30 seconds"))
.count()
)
query = (
counts.writeStream.outputMode("append")
.format("console")
.option("checkpointLocation", "/tmp/rate-counts-checkpoint")
.start()
)
query.awaitTermination()
Sources, sinks and state
Built-in sources include files, Apache Kafka, a socket source for testing and rate sources that generate test data.[3] Built-in sinks include files, Kafka, foreach, foreachBatch, and console and memory sinks for debugging.[3] Connectors for open table formats such as Apache Iceberg and Delta Lake let their tables serve as streaming sources and sinks.[4][5]
Stateful operators such as aggregations, joins and deduplication keep state between batches in a state store. Since Spark 3.2, Spark includes a RocksDB-based state store provider that keeps state in native memory and on local disk instead of the JVM heap.[3]
transformWithState, available since Spark 4.0, is the operator for arbitrary stateful processing. It replaces the older mapGroupsWithState and flatMapGroupsWithState APIs in Scala and applyInPandasWithState in Python, and adds features such as timers and time-to-live (TTL) eviction of state. It is available in Scala, Java and Python.[6]
Execution modes and triggers
By default, Structured Streaming runs a query as a series of small batch jobs (micro-batches), with end-to-end latencies as low as about 100 milliseconds.[1] The trigger controls timing:[3]
| Trigger | Behaviour |
|---|---|
| Unspecified (default) | Starts the next micro-batch as soon as the previous one finishes. |
Fixed interval (processingTime) |
Starts micro-batches at a set interval; skips a batch when there is no new data. |
Available-now (availableNow) |
Processes all data available at start, possibly in several micro-batches, then stops. |
| One-time | Processes available data in one micro-batch, then stops. Deprecated in favour of available-now. |
| Continuous | Experimental low-latency mode, added in Spark 2.3, with at-least-once guarantees.[1] |
| Real-time | Real-time Mode, added in Spark 4.1.0 (see below). |
Real-time Mode runs long-lived tasks, one per input partition, that process records as they arrive instead of waiting for a batch boundary. The Spark documentation states that this lowers end-to-end latency from the micro-batch floor to roughly the time needed to process and send one record. The trigger's duration (default 5 minutes) then mainly sets how often the query checkpoints.[7] Spark 4.1.0 supported stateless queries in this mode.[8] Spark 4.3.0 added streaming aggregation, dropDuplicates and the Scala and Java transformWithState operator, using a pipelined shuffle; stream-stream joins and flatMapGroupsWithState are not yet supported in this mode.[7][9] In PySpark, the mode is enabled with .trigger(realTime="5 minutes").[7]
History
Spark 2.0.0 shipped the first, experimental release of Structured Streaming as a higher-level streaming API on Spark SQL and the Catalyst optimizer.[10] Spark 2.2.0 removed the experimental label and declared the APIs generally available (SPARK-20844).[11] Spark 4.0.0 added the Arbitrary State API v2 (transformWithState) and a State Data Source for reading state when debugging.[12] Real-time Mode followed in 4.1.0 (SPARK-53736) and gained stateful queries in 4.3.0.[8][9]
The older Spark Streaming API, based on discretized streams (DStreams), is described in the Spark 4.3 documentation as the previous generation of Spark's streaming engine; it receives no further updates, and the documentation directs new work to Structured Streaming.[13]
Use in an open lakehouse
In an open lakehouse, Structured Streaming typically ingests events from a message bus into bronze tables and keeps downstream tables current. Spark Declarative Pipelines builds on it: SDP streaming tables are filled by Structured Streaming flows, and SDP can read from any source that spark.readStream supports.[14]
See also
References
- ↑ 1.0 1.1 1.2 1.3 Structured Streaming Programming Guide (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 2.0 2.1 Structured Streaming: Getting Started (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 3.0 3.1 3.2 3.3 3.4 Structured Streaming: APIs on DataFrames and Datasets (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Spark Structured Streaming. Apache Iceberg.Retrieved 2026-11-22.
- ↑ Table streaming reads and writes. Delta Lake.Retrieved 2026-11-22.
- ↑ Structured Streaming: transformWithState (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 7.0 7.1 7.2 Real-time Mode (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 8.0 8.1 Spark Release 4.1.0. Apache Software Foundation.Retrieved 2026-11-22.
- ↑ 9.0 9.1 Spark Release 4.3.0. Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Spark Release 2.0.0. Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Spark Release 2.2.0. Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Spark Release 4.0.0. Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Spark Streaming Programming Guide (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.
- ↑ Spark Declarative Pipelines Programming Guide (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-22.