การแบ่งช่วงเวลาและการรวมข้อมูลแบบมีสถานะใน Kafka Streams
เรียนรู้การรวมข้อมูลตามช่วงเวลาและการจัดการคลังสถานะใน Kafka Streams เพื่อคำนวณจำนวนสะสม ผลรวม และเมทริกซ์บนกระแสเหตุการณ์
การแบ่งช่วงเวลาและการรวมข้อมูลแบบมีสถานะใน Kafka Streams เป็นบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส 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 ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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