Kafka Streams 中的窗口与有状态聚合
学习在 Kafka Streams 中执行基于时间窗口的聚合并管理状态存储,以计算事件流中的持续计数、总和和指标。
Kafka Streams 中的窗口与有状态聚合 是 CoddyKit 上的免费 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Stateful vs Stateless
Operations like map and filter are stateless. Aggregations such as counting or summing require state that persists across records.
Kafka Streams manages this state for you in local state stores.
Grouping a Stream
Before aggregating you group records by key with groupByKey or groupBy.
KGroupedStream<String, Order> grouped =
orders.groupBy((key, order) -> order.getCustomerId());Counting per Key
A simple aggregation: count orders per customer. The result is a KTable backed by a state store.
KTable<String, Long> counts = grouped.count();General Aggregation
Use aggregate for custom accumulation. You provide an initializer and an adder function.
KTable<String, Double> totals = grouped.aggregate(
() -> 0.0,
(key, order, sum) -> sum + order.getAmount());Introducing Windows
Often you want aggregations over a time window, e.g. orders in the last 5 minutes. Kafka Streams supports tumbling, hopping, and session windows.
Tumbling Windows
A tumbling window is fixed-size and non-overlapping. Each record belongs to exactly one window.
grouped
.windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofMinutes(5)))
.count();Hopping Windows
Hopping windows have a size and a smaller advance, so they overlap. A record can fall into multiple windows.
TimeWindows.ofSizeAndGrace(Duration.ofMinutes(5), Duration.ofSeconds(30))
.advanceBy(Duration.ofMinutes(1));Session Windows
Session windows group records separated by gaps of inactivity — ideal for user sessions where activity bursts then pauses.
grouped
.windowedBy(SessionWindows.ofInactivityGapWithNoGrace(Duration.ofMinutes(10)))
.count();State Store Fault Tolerance
State stores are backed by compacted changelog topics in Kafka. If an instance fails, its state is rebuilt from the changelog on another instance.
Querying State Interactively
Interactive Queries let you read state store values directly from the application, exposing aggregations via a REST endpoint without an external database.
ReadOnlyKeyValueStore<String, Long> store =
streams.store(StoreQueryParameters.fromNameAndType(
"counts", QueryableStoreTypes.keyValueStore()));Putting It Together
Windowed, stateful aggregations turn raw event streams into live metrics. Group by key, choose a window type, aggregate, and optionally expose the state via interactive queries.
Quick Check
Test your understanding of windowing.
Recap
You learned windowing and stateful aggregations.
- Group with
groupByKeybefore aggregating. - Use
countoraggregateto build a KTable. - Tumbling, hopping, and session windows handle time differently.
- State stores are fault-tolerant via changelog topics.
常见问题解答
「Kafka Streams 中的窗口与有状态聚合」课时是免费的吗?
是的 — 「Kafka Streams 中的窗口与有状态聚合」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程的其余内容,请升级到 CoddyKit PRO。 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程共包含 4 节课。
「Kafka Streams 中的窗口与有状态聚合」这节课中我会学到什么?
学习在 Kafka Streams 中执行基于时间窗口的聚合并管理状态存储,以计算事件流中的持续计数、总和和指标。 你通过在浏览器中直接运行的动手代码来练习 Advanced Spring Boot 4: Event-Driven Architecture (Kafka),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「Kafka Streams 中的窗口与有状态聚合」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课中编写并运行代码吗?
能。每节 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Kafka Streams 简介
- 使用 KStream 和 KTable 进行流处理
- 构建简单的流应用
- Kafka Streams 中的窗口与有状态聚合