# 事件时间

**事件时间**是[流处理](https://www.edgepedia.org/zh/stream-processing)中的一个时间概念，表示事件在其产生设备或源处实际发生的时间，而不是处理系统观察到或处理它的时间。[时间戳](https://www.edgepedia.org/zh/timestamp)通常嵌入在事件记录本身中，例如汽车中的 GPS 传感器捕获位置变化的时间，系统用它来对数据排序、定义窗口，并计算不依赖于计算实际运行时间的结果。

| 关键事实 | 详情 |
|---|---|
| 定义 | 事件“在源处”发生的时间点，通常在数据记录产生时嵌入其中<sup>[1](https://kafka.apache.org/35/documentation/streams/core-concepts/)</sup> |
| 与处理时间的对比 | 处理时间是记录恰好被消费或处理的时间；它可能比事件时间晚几毫秒、几小时或几天<sup>[1](https://kafka.apache.org/35/documentation/streams/core-concepts/)</sup> |
| 第三个概念，摄入时间 | 事件进入流平台或系统的时间；在 Kafka 中，从 Kafka 0.10.x 开始，由 broker 在记录追加到主题分区时打上时间戳<sup>[1](https://kafka.apache.org/35/documentation/streams/core-concepts/)</sup> |
| 确定性 | 给定相同数据，事件时间的结果是一致的、可重复的；处理时间是非确定性的，因为它取决于到达速率、算子速度和故障<sup>[2](https://nightlies.apache.org/flink/flink-docs-release-1.6/dev/event_time.html)</sup> |
| 完整性机制 | 水位线是事件时间标记，指示事件已摄入到哪个点，让系统决定窗口何时可以产生完整、可重复的结果<sup>[3](https://learn.microsoft.com/en-us/azure/stream-analytics/stream-analytics-time-handling)</sup> |
| 典型代价 | 事件时间处理会因等待乱序事件而产生延迟，因为只能进行有限等待<sup>[2](https://nightlies.apache.org/flink/flink-docs-release-1.6/dev/event_time.html)</sup> |
| 系统中的选择 | Apache Flink 声明时间域和水位线策略；Azure Stream Analytics 默认使用到达时间，并使用 Timestamp by 子句指定事件时间，以 SYSTEM.TIMESTAMP 暴露<sup>[2](https://nightlies.apache.org/flink/flink-docs-release-1.6/dev/event_time.html)</sup><sup> • </sup><sup>[3](https://learn.microsoft.com/en-us/azure/stream-analytics/stream-analytics-time-handling)</sup> |

## 三个时间域

流处理框架区分多种时钟。Flink 明确支持三种：事件时间，即产生事件的设备记录的时间；摄入时间，即 Flink 在摄入事件时记录的时间戳；以及处理时间，即特定算子处理事件的时间<sup>[2](https://nightlies.apache.org/flink/flink-docs-release-1.6/dev/event_time.html)</sup>。[Kafka Streams](https://www.edgepedia.org/zh/kafka-streams) 描述了同样的三元组，并指出在实时管道中处理时间可能比事件时间晚几毫秒或几秒，而批处理管道可能滞后数小时<sup>[1](https://kafka.apache.org/35/documentation/streams/core-concepts/)</sup>。一项关于流处理系统的调研同样将事件（应用）时间、摄入时间和处理时间列为现代数据流系统的三种运行模式<sup>[4](https://link.springer.com/article/10.1007/s00778-023-00819-8)</sup>。

这三者回答不同的问题。事件时间问的是业务事实何时发生；处理时间问的是系统何时处理它<sup>[5](https://techhub.cafe/interview-questions/data-processing/event-time-vs-processing-time)</sup>。一笔在 12:00:01 从离线设备发起的支付可能到 12:03:20 才到达 Kafka 消费者；只有事件时间戳记录了原始时刻<sup>[5](https://techhub.cafe/interview-questions/data-processing/event-time-vs-processing-time)</sup>。

**摄入时间介于两者之间。** 由于其时间戳在源处一次性分配并且稳定，对相同记录的不同窗口操作引用相同的时间戳；而使用处理时间时，每个窗口算子可能根据本地时钟和传输延迟将记录分配到不同的窗口<sup>[6](https://apache.googlesource.com/flink/%2B/refs/heads/blink/docs/dev/event_time.md)</sup>。Kafka 在 broker 处实现摄入时间戳，其默认的 Kafka Streams 时间戳提取器按原样检索嵌入的 Kafka 时间戳<sup>[1](https://kafka.apache.org/35/documentation/streams/core-concepts/)</sup>。

## 为什么使用事件时间

在事件时间中，时间的推进取决于数据，而不是任何墙上时钟。假设所有数据都已到达，一个按小时的事件时间窗口包含所有事件时间戳落在该小时内的记录，无论到达顺序或处理时间如何，并且在重新处理历史数据时行为同样成立<sup>[2](https://nightlies.apache.org/flink/flink-docs-release-1.6/dev/event_time.html)</sup>。某只股票在给定交易日第一个小时内的最高价，无论计算何时运行，结果都相同<sup>[7](https://apache.googlesource.com/flink/%2Bshow/c955eb3a47cd1d0e33dc42d4e9cb9c7b6ad26582/docs/training/streaming_analytics.md)</sup>。

相比之下，处理时间对在某个时间间隔内恰好被处理的事件进行计算。它是最简单的概念，不需要机器之间的协调，并提供最低的延迟，但其结果会随到达速率、处理速度和故障而变化<sup>[2](https://nightlies.apache.org/flink/flink-docs-release-1.6/dev/event_time.html)</sup>。它适用于具有严格低延迟要求且能容忍近似结果的应用，也可用于早期、不完整的触发，以暗示最终的事件时间结果，因为在实时管道中，一小时的事件时间通常接近一小时的墙上时间<sup>[8](https://flink.apache.org/what-is-flink/flink-applications/)</sup><sup> • </sup><sup>[9](https://apache.googlesource.com/flink/%2B/master/docs/content/docs/dev/datastream/execution_mode.md)</sup>。

事件时间实际上是重放历史所必需的。在处理时间上重新处理事件流过去四周的数据，会使处理器将这四周的数据当作在几分钟内产生，破坏了数据原有的时间分布<sup>[10](https://developer.confluent.io/patterns/stream-processing/event-time-processing/)</sup>。选择摄入时间或处理时间的一个原因，是事件源不可信，无法提供可靠的嵌入时间戳<sup>[10](https://developer.confluent.io/patterns/stream-processing/event-time-processing/)</sup>。

## 乱序到达与水位线

流记录通常乱序到达，即晚于具有更晚事件时间戳的记录到达，因为网络路由使某些记录比其他记录传递得更快<sup>[4](https://link.springer.com/article/10.1007/s00778-023-00819-8)</sup>。移动设备是典型情况：一个比先前事件晚几秒创建的事件可能先被处理，因为它经历的延迟更小<sup>[11](https://developer.confluent.io/courses/apache-flink/timely-stream-processing/)</sup>。

水位线是让系统推断事件时间进展的机制。Azure Stream Analytics 将其定义为事件时间标记，指示处理器已摄入事件到哪个点；水位线允许系统产生完整、正确、可重复且无需撤回的结果，并在错误处理期间为重新计数提供安全的起点和终点<sup>[3](https://learn.microsoft.com/en-us/azure/stream-analytics/stream-analytics-time-handling)</sup>。Flink 要求事件时间程序指定水位线的生成方式，因为水位线标志着事件时间的进展<sup>[2](https://nightlies.apache.org/flink/flink-docs-release-1.6/dev/event_time.html)</sup>。

由于没有有限观察能证明不会再有记录到达，水位线是一种启发式断言，即时间 T 之前的所有事件都已到达<sup>[12](https://www.thedataengineering.com/mechanisms/time-semantics/how-it-works/watermarks)</sup>。两种常见的启发式假设有界乱序：当事件时间为 t 的元素到达时，节点将水位线推进到 t − Δ，其中 Δ 是可配置的常数；或者在固定超时后推进。这两种方法在系统运行良好时可能引入不必要的延迟，而在出现问题时延迟过小，产生大量迟到数据；更复杂的方法对源进行统计建模并自适应调整水位线延迟<sup>[13](https://vldb.org/pvldb/vol14/p3135-begoli.pdf)</sup>。

## 迟到数据与延迟权衡

在其窗口处理完成之后到达的事件是迟到事件。Flink 提供多种处理选项，例如通过侧输出重新路由它们，或更新先前已完成的结果<sup>[8](https://flink.apache.org/what-is-flink/flink-applications/)</sup>。五分钟的水位线意味着系统假定任何事件时间戳比当前处理时间落后超过五分钟的事件对其原始窗口来说都太迟了<sup>[14](https://tdwi.org/blogs/data-101/2026/05/what-is-late-arriving-data.aspx)</sup>。

选择水位线延迟是在延迟与完整性之间取得平衡：将基于分位数的延迟从 p99 收紧到 p99.99，可将迟到记录率从 1% 降至 0.01%，迟到记录减少到原来的百分之一，代价是等待更久<sup>[12](https://www.thedataengineering.com/mechanisms/time-semantics/how-it-works/watermarks)</sup>。迟到数据还有状态开销：按一分钟事件时间窗口计数需要记住自应用启动以来每个一分钟窗口的计数，因为任何窗口仍可能收到迟到记录；水位线决定有状态算子何时可以忘记旧状态<sup>[15](https://www2.eecs.berkeley.edu/Pubs/TechRpts/2018/EECS-2018-26.pdf)</sup>。

## 系统如何暴露事件时间

在大多数系统中，必须显式选择事件携带的时间戳。在 Azure Stream Analytics 中，到达时间是默认值，对于事件中心输入可通过 EventEnqueuedUtcTime 获取，对于 IoT 中心可通过 IoTHub.EnqueuedTime 获取，对于 blob 输入可通过 BlobProperties.LastModified 获取；查询中的 Timestamp by 子句将处理切换为负载中嵌入的应用时间，分配的时间可作为 SYSTEM.TIMESTAMP 读取<sup>[3](https://learn.microsoft.com/en-us/azure/stream-analytics/stream-analytics-time-handling)</sup>。Flink SQL 在表级别定义水位线策略，例如发出观察到的最大事件时间戳减去 30 秒<sup>[10](https://developer.confluent.io/patterns/stream-processing/event-time-processing/)</sup>。

事件时间滞后也是一个被监控的量。[Google Cloud Dataflow](https://www.edgepedia.org/zh/google-cloud-dataflow) 的数据新鲜度指标是元素被处理的时间与该元素的事件时间戳之间的差值；水位线时间与事件时间之间的巨大差距可能表明操作缓慢或卡住<sup>[16](https://docs.cloud.google.com/dataflow/docs/guides/using-monitoring-intf)</sup>。[Apache Beam](https://www.edgepedia.org/zh/apache-beam) 中的触发器基于此模型构建，可在事件时间、处理时间、数据属性上触发，或作为复合触发器，以权衡完整性、延迟和成本，并处理在水位线越过窗口结束之后到达的数据<sup>[17](https://beam.apache.org/documentation/basics/)</sup>。

## References

1. [Core Concepts, Apache Kafka Streams documentation](https://kafka.apache.org/35/documentation/streams/core-concepts/)
2. [Event Time / Processing Time / Ingestion Time, Apache Flink 1.6 documentation](https://nightlies.apache.org/flink/flink-docs-release-1.6/dev/event_time.html)
3. [Understand time handling in Azure Stream Analytics, Microsoft Learn](https://learn.microsoft.com/en-us/azure/stream-analytics/stream-analytics-time-handling)
4. [Fragkoulis, Carbone, Kalavri, Katsifodimos (2024). A survey on the evolution of stream processing systems. The VLDB Journal 33, 507–541](https://link.springer.com/article/10.1007/s00778-023-00819-8)
5. [What is event time vs processing time in stream processing?, techhub.cafe](https://techhub.cafe/interview-questions/data-processing/event-time-vs-processing-time)
6. [Event Time / Processing Time / Ingestion Time, Apache Flink (Blink branch) documentation](https://apache.googlesource.com/flink/%2B/refs/heads/blink/docs/dev/event_time.md)
7. [Streaming Analytics, Apache Flink training docs](https://apache.googlesource.com/flink/%2Bshow/c955eb3a47cd1d0e33dc42d4e9cb9c7b6ad26582/docs/training/streaming_analytics.md)
8. [Applications, Apache Flink project site](https://flink.apache.org/what-is-flink/flink-applications/)
9. [Execution Mode (Batch/Streaming), Apache Flink documentation](https://apache.googlesource.com/flink/%2B/master/docs/content/docs/dev/datastream/execution_mode.md)
10. [Event-Time Processing, Confluent developer patterns](https://developer.confluent.io/patterns/stream-processing/event-time-processing/)
11. [Event Time and Watermarks, Confluent Flink course](https://developer.confluent.io/courses/apache-flink/timely-stream-processing/)
12. [Watermarks: declaring completeness, The Data Engineering](https://www.thedataengineering.com/mechanisms/time-semantics/how-it-works/watermarks)
13. [Begoli et al. Watermarks in Stream Processing Systems, PVLDB Vol. 14](https://vldb.org/pvldb/vol14/p3135-begoli.pdf)
14. [What Is Late-Arriving Data and How Do You Handle It?, TDWI](https://tdwi.org/blogs/data-101/2026/05/what-is-late-arriving-data.aspx)
15. [EECS-2018-26, UC Berkeley technical report on bounded lateness](https://www2.eecs.berkeley.edu/Pubs/TechRpts/2018/EECS-2018-26.pdf)
16. [Dataflow job metrics, Google Cloud documentation](https://docs.cloud.google.com/dataflow/docs/guides/using-monitoring-intf)
17. [Basics of the Beam model, Apache Beam documentation](https://beam.apache.org/documentation/basics/)

---
*Initially written Oct 26, 2026*

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

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/event-time

English original: https://www.edgepedia.org/event-time
