# Apache Spark 结构化流

**Apache Spark 结构化流**（Apache Spark Structured Streaming）是一个可扩展、容错的流处理引擎，构建在 Spark SQL 引擎之上。在其中，流式计算用与批处理数据相同的 Dataset 与 DataFrame API 来表达，引擎则在有新数据到达时增量地、持续地执行它。<sup>[1](https://spark.apache.org/docs/latest/streaming/index.html)</sup> 它最初以实验性 API 的形式随 [Apache Spark](https://www.edgepedia.org/zh/apache-spark) 2.0 引入，构建于 Spark SQL 与 Catalyst 优化器之上，取代了更早的、基于 DStream 的 Spark Streaming 引擎——后者如今已是遗留技术。<sup>[2](https://spark.apache.org/releases/spark-release-2-0-0.html)</sup><sup> • </sup><sup>[3](https://spark.apache.org/docs/latest/streaming-programming-guide)</sup>

| 关键事实 | 详情 |
|---|---|
| 引入时间 | 作为实验性版本随 Apache Spark 2.0 发布，构建于 Spark SQL 与 Catalyst 优化器之上<sup>[2](https://spark.apache.org/releases/spark-release-2-0-0.html)</sup> |
| 编程模型 | 流被建模为一张无界表；查询用 Scala、Java、Python 或 R 中的 Dataset/DataFrame API 编写<sup>[1](https://spark.apache.org/docs/latest/streaming/index.html)</sup><sup> • </sup><sup>[4](https://learn.microsoft.com/en-us/fabric/data-engineering/structured-streaming-overview)</sup> |
| 默认执行方式 | 微批引擎，把流当作一系列小的批作业处理，端到端延迟可低至 100 毫秒，并提供恰好一次保证<sup>[1](https://spark.apache.org/docs/latest/streaming/index.html)</sup> |
| 低延迟执行方式 | 持续处理（Continuous Processing），自 Spark 2.3 起可用，端到端延迟可低至 1 毫秒，提供至少一次保证<sup>[1](https://spark.apache.org/docs/latest/streaming/index.html)</sup> |
| 实时模式 | Spark 4.1.0（2025 年 12 月）为无状态 Scala 查询加入官方支持，并在 Spark 4.2.0（2026 年 7 月）扩展至 PySpark；无状态任务可达到个位数毫秒级延迟<sup>[5](https://spark.apache.org/releases/spark-release-4.1.0.html)</sup><sup> • </sup><sup>[6](https://answers.databricks.com/new-spark-structured-streaming-spark)</sup> |
| 容错 | 通过检查点与预写日志实现端到端的恰好一次保证<sup>[1](https://spark.apache.org/docs/latest/streaming/index.html)</sup> |

## 编程模型

结构化流把输入流建模为一张无界表，新行不断追加到其中。一个查询由三部分组成：读取新输入记录的源（source）、定义逻辑的转换（transformation），以及写出输出行的汇（sink）。该模型是声明式的：开发者只描述结果，Spark SQL 引擎则在输入可用时规划反复的增量执行。<sup>[4](https://learn.microsoft.com/en-us/fabric/data-engineering/structured-streaming-overview)</sup> 最典型的例子是流式词频统计：通过 `spark.readStream` 读取一个套接字源，用 SQL 函数切分成单词，再像任何静态 DataFrame 那样用 `groupBy("word").count()` 进行聚合。<sup>[7](https://spark.apache.org/docs/latest/streaming/getting-started.html)</sup>

<下划线并非处处需要，但引擎会在查询启动时校验查询计划，并报告那些不支持流式输入的操作，因为并非每一种批处理操作都适用于无界数据。<sup>[4](https://learn.microsoft.com/en-us/fabric/data-engineering/structured-streaming-overview)</sup>

**源与汇。** 内置的源包括：文件源，读取以原子方式放入目录中的文件（文本、CSV、JSON、ORC 与 Parquet 格式）；Kafka 源，兼容 0.10.0 及以上版本的消息代理；以及用于测试的套接字源和速率源。<sup>[8](https://spark.apache.org/docs/latest/streaming/apis-on-dataframes-and-datasets.html)</sup> 汇则把输出行写入存储、内存或其他目的地。<sup>[4](https://learn.microsoft.com/en-us/fabric/data-engineering/structured-streaming-overview)</sup>

## 执行模式

**微批模式。** 默认情况下，查询运行在微批引擎上，把流作为一系列小的批作业来处理，端到端延迟可低至 100 毫秒，并提供恰好一次的容错保证。该模式面向分析型流水线与 ETL——在这类场景中，延迟以秒或分钟计，成本效率更为重要。<sup>[1](https://spark.apache.org/docs/latest/streaming/index.html)</sup><sup> • </sup><sup>[9](https://spark.apache.org/streaming/)</sup>

**持续处理与实时模式。** 自 Spark 2.3 起，持续处理模式已实现低至 1 毫秒的端到端延迟，保证为至少一次而非恰好一次。<sup>[1](https://spark.apache.org/docs/latest/streaming/index.html)</sup> 当前的实时模式（Real-time Mode，RTM）最早在 Spark 4.1.0 中为无状态 Scala 查询提供官方支持，并在 Spark 4.2.0 中扩展至 PySpark；它采用持续处理，配合流水线化混洗与流式混洗，为无状态工作负载（如在线欺诈检测、实时个性化与告警）达到个位数毫秒级延迟。<sup>[5](https://spark.apache.org/releases/spark-release-4.1.0.html)</sup><sup> • </sup><sup>[6](https://answers.databricks.com/new-spark-structured-streaming-spark)</sup><sup> • </sup><sup>[9](https://spark.apache.org/streaming/)</sup> RTM 要求使用更新输出模式，并只支持一个明确的源、算子与汇的子集；处在该子集内的查询，基本只需更换触发器即可在微批执行与实时执行之间切换，例如用 `Trigger.RealTime(...)` 替代 `Trigger.ProcessingTime(...)`，而无须重写查询逻辑。<sup>[9](https://spark.apache.org/streaming/)</sup> Databricks 的文档称，实时模式在记录到达时即行处理，端到端延迟可低至五毫秒。<sup>[10](https://docs.databricks.com/aws/en/structured-streaming/concepts)</sup>

**调度式增量处理。** 同样的 `readStream` 与 `writeStream` API 也支持 `AvailableNow` 触发器，它会处理查询启动时所有可用的数据，然后停止。调度器之后可以用同一个检查点重启查询，因此增量 ETL、回填与追赶作业能复用常驻流式作业所用的进度跟踪与有状态计算。<sup>[4](https://learn.microsoft.com/en-us/fabric/data-engineering/structured-streaming-overview)</sup>

## 有状态操作、水位线与输出模式

聚合、去重、连接以及自定义有状态逻辑这类有状态操作，会在多次触发之间维护状态，并通过流式检查点加以跟踪。<sup>[4](https://learn.microsoft.com/en-us/fabric/data-engineering/structured-streaming-overview)</sup> **水位线**（watermark）在有状态操作之前通过 `withWatermark` 设置在事件时间列上，它告诉 Spark 数据预期会迟到多久；它决定了何时可以清除旧状态，以及何时迟到的行已太旧、不能再更新结果。在配置的延迟之内到达的数据不会被丢弃，而比水位线更旧的数据仍可能被处理，但得不到保证。水位线只在新输入被处理时推进，因此若无数据到达，窗口输出、未匹配的外连接行以及状态清理都可能被推迟。<sup>[11](https://learn.microsoft.com/fabric/data-engineering/structured-streaming-stateful-processing)</sup> 选择水位线延迟是一个权衡：延迟太短会丢弃有效的迟到数据，延迟太长则保留更多状态、增大检查点体积。<sup>[11](https://learn.microsoft.com/fabric/data-engineering/structured-streaming-stateful-processing)</sup>

带状态聚合的查询可在三种**输出模式**中选择：追加模式（默认），算子只输出未来处理不会再改变的行，由水位线来决定；更新模式，算子在本次触发中输出所有发生变化的行，即使它们之后还会再变；以及完整模式，它只适用于流式聚合，输出迄今为止产生过的所有行。<sup>[12](https://docs.databricks.com/aws/en/structured-streaming/output-mode)</sup> 连接只支持追加模式，而任意有状态算子 `mapGroupsWithState` 与 `flatMapGroupsWithState` 通过各自的逻辑输出记录，不受输出模式影响。<sup>[12](https://docs.databricks.com/aws/en/structured-streaming/output-mode)</sup> 对于无状态流，所有输出模式的行为都相同。<sup>[12](https://docs.databricks.com/aws/en/structured-streaming/output-mode)</sup>

## 容错与语义

系统通过检查点与预写日志提供端到端的恰好一次保证。<sup>[1](https://spark.apache.org/docs/latest/streaming/index.html)</sup> 引入时所述的设计目标是一条强语义保证：在任何时刻，应用程序的输出都等价于在数据的一个前缀上执行一次批作业，而引擎在内部以及与外部系统交互时都会处理一致性、容错与乱序数据。<sup>[13](https://www.databricks.com/blog/2016/07/28/structured-streaming-in-apache-spark.html)</sup> 检查点兼容性在实践中很重要：只有当查询保持检查点兼容时，检查点才应被复用；Spark 4.2 为流式源加入了稳定名称，使查询可以增删或重排源而不破坏其检查点。<sup>[4](https://learn.microsoft.com/en-us/fabric/data-engineering/structured-streaming-overview)</sup><sup> • </sup><sup>[6](https://answers.databricks.com/new-spark-structured-streaming-spark)</sup>

## 相对于其他引擎的性能

2020 年发表的一项 IEEE 基准研究比较了结构化流、Spark Streaming、Flink 与 [Kafka Streams](https://www.edgepedia.org/zh/kafka-streams)，发现结构化流的微批架构能以高延迟为代价处理极高的吞吐量；微批在延迟上的劣势对无状态操作最为明显；在严格的延迟 SLA 下，Flink 能维持最高的吞吐量，而微批系统在启动时一波数据到达之后追赶得更快。<sup>[14](https://ieeexplore.ieee.org/abstract/document/9025240)</sup>

## 历史与近期发展

该引擎的发展经历了几个不同阶段。Spark 2.0 于 2016 年发布了最初的实验性版本。<sup>[2](https://spark.apache.org/releases/spark-release-2-0-0.html)</sup> 持续处理在 Spark 2.3 中到来。<sup>[1](https://spark.apache.org/docs/latest/streaming/index.html)</sup> 在 4.x 系列中，Spark 4.0.0（2025 年 5 月）引入了新的任意有状态算子 `transformWithState`，以及一个实验性的状态数据源（State Data Source），可从检查点读取查询的状态；Spark 4.1.0（2025 年 12 月 16 日）为无状态 Scala 查询加入了实时模式；Spark 4.2.0（2026 年 7 月 14 日）把实时模式带到 PySpark，并引入了具名流式源。<sup>[6](https://answers.databricks.com/new-spark-structured-streaming-spark)</sup><sup> • </sup><sup>[15](https://answers.databricks.com/changed-spark-users-2026)</sup> 结构化流作为 Apache Spark 的一部分开发，随每个 Spark 版本接受测试与更新；较早的、基于 DStream 的 Spark Streaming 引擎则不再获得更新。<sup>[9](https://spark.apache.org/streaming/)</sup><sup> • </sup><sup>[3](https://spark.apache.org/docs/latest/streaming-programming-guide)</sup>

## References

1. [Structured Streaming Programming Guide, Apache Spark 4.2.0 Documentation (overview)](https://spark.apache.org/docs/latest/streaming/index.html)
2. [Spark Release 2.0.0, Apache Spark](https://spark.apache.org/releases/spark-release-2-0-0.html)
3. [Spark Streaming Programming Guide (legacy), Apache Spark](https://spark.apache.org/docs/latest/streaming-programming-guide)
4. [Overview of Structured Streaming, Microsoft Learn (Fabric)](https://learn.microsoft.com/en-us/fabric/data-engineering/structured-streaming-overview)
5. [Spark Release 4.1.0, Apache Spark](https://spark.apache.org/releases/spark-release-4.1.0.html)
6. [What's new in Spark Structured Streaming in Spark 4.x?, Databricks](https://answers.databricks.com/new-spark-structured-streaming-spark)
7. [Structured Streaming Programming Guide: Getting Started, Apache Spark 4.2.0 Documentation](https://spark.apache.org/docs/latest/streaming/getting-started.html)
8. [Structured Streaming Programming Guide: APIs on DataFrames and Datasets, Apache Spark 4.2.0 Documentation](https://spark.apache.org/docs/latest/streaming/apis-on-dataframes-and-datasets.html)
9. [Spark Structured Streaming, Apache Spark project page](https://spark.apache.org/streaming/)
10. [Structured Streaming concepts, Databricks on AWS](https://docs.databricks.com/aws/en/structured-streaming/concepts)
11. [Stateful processing with Structured Streaming, Microsoft Learn (Fabric)](https://learn.microsoft.com/fabric/data-engineering/structured-streaming-stateful-processing)
12. [Select an output mode for Structured Streaming, Databricks on AWS](https://docs.databricks.com/aws/en/structured-streaming/output-mode)
13. [Zaharia, Das, Lumb, and Xin (2016). Spark Structured Streaming, Databricks blog](https://www.databricks.com/blog/2016/07/28/structured-streaming-in-apache-spark.html)
14. [Evaluation of Stream Processing Frameworks, IEEE Transactions on Parallel and Distributed Systems, 2020](https://ieeexplore.ieee.org/abstract/document/9025240)
15. [What changed for Spark users in 2026?, Databricks](https://answers.databricks.com/changed-spark-users-2026)

---
*Initially written Oct 26, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Data mining, warehousing, and big data › Big data platforms and frameworks

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/apache-spark-structured-streaming

English original: https://www.edgepedia.org/apache-spark-structured-streaming
