0
0
Kafkadevops~30 mins

Windowed operations in Kafka - Mini Project: Build & Apply

Choose your learning style9 modes available
Windowed Operations with Kafka Streams
📖 Scenario: You are working with a stream of sales data coming from a Kafka topic. Each record contains a product name and the number of units sold. You want to calculate the total units sold for each product in 1-minute time windows.
🎯 Goal: Build a Kafka Streams application that reads sales data, groups it by product name, applies a 1-minute tumbling window, sums the units sold per product in each window, and outputs the results.
📋 What You'll Learn
Create a Kafka Streams builder and define the input stream from topic sales.
Create a 1-minute tumbling windowed aggregation grouped by product name.
Sum the units sold per product in each window.
Print the windowed aggregation results to the console.
💡 Why This Matters
🌍 Real World
Windowed operations are used in real-time analytics to summarize data over fixed time periods, such as counting sales per minute or monitoring sensor data.
💼 Career
Understanding windowed aggregations in Kafka Streams is essential for building scalable, real-time data processing applications in roles like data engineer or streaming developer.
Progress0 / 4 steps
1
Create the initial stream from the sales topic
Create a Kafka Streams builder called builder and define a stream called salesStream from the topic "sales" with key type String and value type Integer.
Kafka
Need a hint?

Use StreamsBuilder to create the builder and builder.stream("sales") to get the stream.

2
Define a 1-minute tumbling window for grouping
Create a variable called windowSize of type Duration and set it to 1 minute. Then create a TimeWindows object called tumblingWindow using TimeWindows.ofSizeWithNoGrace(windowSize).
Kafka
Need a hint?

Use Duration.ofMinutes(1) for the window size and TimeWindows.ofSizeWithNoGrace(windowSize) for the tumbling window.

3
Group by product and sum units sold in each window
Group salesStream by key using groupByKey(), then apply a windowed aggregation using windowedBy(tumblingWindow) and sum the values using reduce(Integer::sum). Store the result in a variable called windowedSales of type KTable<Windowed<String>, Integer>.
Kafka
Need a hint?

Use groupByKey() then windowedBy(tumblingWindow) and reduce(Integer::sum) to sum units sold per window.

4
Print the windowed aggregation results
Use windowedSales.toStream() and then foreach() to print each window's product name, window start time, and total units sold. Use System.out.println inside the lambda to print in the format: "Product: {key}, Window start: {windowStart}, Total units: {value}".
Kafka
Need a hint?

Use toStream() and foreach() to print each windowed result with System.out.println.