流处理中的时间语义
理解事件时间、处理时间和摄取时间,并了解选择正确的时间语义为何对获得正确的流处理结果至关重要。
流处理中的时间语义 是 CoddyKit 上的免费 Apache Kafka & Stream Processing Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Apache Kafka & Stream Processing Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Time Matters
In stream processing, when an event happened is often more important than when you processed it.
Choosing the wrong notion of time leads to incorrect counts, broken windows, and misleading analytics.
Event Time
Event time is the timestamp embedded in the event itself — when it actually occurred at the source.
- A purchase made at 14:03 carries 14:03 regardless of network delays.
- It produces deterministic, replayable results.
Processing Time
Processing time is the wall-clock time of the machine running the stream operator when it sees the event.
- Simple and low-latency.
- But non-deterministic — the same data reprocessed later yields different windows.
Ingestion Time
Ingestion time is when the event entered the streaming system (e.g., appended to a Kafka topic).
It is a middle ground: more stable than processing time, but still not the true moment the event occurred.
Comparing the Three
For one event, the order is usually:
- Event time (created at source)
- then ingestion time (arrives in the system)
- then processing time (operator reads it)
The gaps between them are caused by network and queueing delays.
The Out-of-Order Problem
Events rarely arrive in event-time order. A mobile device offline for an hour may deliver events long after they occurred.
If you window by event time, the engine must wait for and correctly slot these late arrivals.
Watermarks
A watermark is the engine's estimate that no events older than time T will still arrive.
- It lets the system decide when an event-time window is complete.
- It trades latency for completeness — wait longer, catch more late events.
Extracting Event Time
To use event time, you tell the engine how to read the timestamp from each record's payload.
{
"orderId": "A-1001",
"amount": 42.50,
"eventTime": "2026-05-31T14:03:00Z"
}Choosing a Semantic
Pick based on requirements:
- Event time — analytics, billing, anything needing correctness and replayability.
- Processing time — real-time monitoring where approximate is fine.
- Ingestion time — when source timestamps are unreliable.
Allowed Lateness
Most engines let you configure allowed lateness — a grace period after the watermark during which late events still update results.
Events arriving after that are dropped or routed to a side output for separate handling.
Putting It Together
Correct time handling means:
- Carry an event-time timestamp in every record.
- Use watermarks to know when windows are done.
- Set allowed lateness for stragglers.
- Prefer event time for any result that must be reproducible.
Quick Check
Test your understanding of time semantics.
Recap
You learned the three core time semantics.
- Event time = when it happened; processing time = when read; ingestion time = when it entered the system.
- Watermarks decide when event-time windows are complete.
- Allowed lateness handles stragglers.
- Use event time for correctness and replayability.
常见问题解答
「流处理中的时间语义」课时是免费的吗?
是的 — 「流处理中的时间语义」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Apache Kafka & Stream Processing Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
「流处理中的时间语义」这节课中我会学到什么?
理解事件时间、处理时间和摄取时间,并了解选择正确的时间语义为何对获得正确的流处理结果至关重要。 你通过在浏览器中直接运行的动手代码来练习 Apache Kafka & Stream Processing Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Apache Kafka & Stream Processing Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Apache Kafka & Stream Processing Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「流处理中的时间语义」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Apache Kafka & Stream Processing Fundamentals 课中编写并运行代码吗?
能。每节 Apache Kafka & Stream Processing Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。