What does the GroupBy operation do in Kafka Streams?
It groups records by a key so that aggregation functions can be applied to each group separately.
Click to reveal answer
beginner
Name a common aggregation function used after GroupBy in Kafka Streams.
Common aggregation functions include count(), reduce(), and aggregate().
Click to reveal answer
intermediate
What is the difference between reduce() and aggregate() in Kafka Streams?
reduce() combines values of the same type, while aggregate() allows different types for input and output and supports an initializer.
Click to reveal answer
intermediate
Why is it important to use a Materialized state store in aggregation?
It stores the aggregation results so they can be queried or used later, enabling fault tolerance and stateful processing.
Click to reveal answer
beginner
What happens if you perform GroupBy without an aggregation in Kafka Streams?
Grouping alone does not produce output; aggregation is needed to combine grouped records into meaningful results.
Click to reveal answer
In Kafka Streams, which method is used to group records by a new key?
Areduce()
BgroupByKey()
Caggregate()
DgroupBy()
✗ Incorrect
groupBy() is used to group records by a new key, while groupByKey() groups by the existing key.
Which aggregation function counts the number of records in each group?
Acount()
Baggregate()
Cmap()
Dreduce()
✗ Incorrect
count() returns the number of records in each group.
What type of Kafka Streams store is typically used to keep aggregation results?
AWindowStore
BKeyValueStore
CTimestampedKeyValueStore
DInMemoryStore
✗ Incorrect
KeyValueStore is commonly used to store aggregated results for stateful processing.
Which method allows you to initialize an aggregation with a custom starting value?
Acount()
Breduce()
Caggregate()
DgroupBy()
✗ Incorrect
aggregate() lets you provide an initializer for the aggregation.
What is the output type of reduce() in Kafka Streams?
ASame as input value type
BDifferent from input value type
CAlways a Long
DAlways a String
✗ Incorrect
reduce() combines values of the same type, so output type matches input value type.
Explain how GroupBy and aggregation work together in Kafka Streams to process data.
Think about grouping first, then combining.
You got /4 concepts.
Describe the difference between reduce() and aggregate() methods in Kafka Streams aggregation.
Focus on input/output types and initialization.
You got /4 concepts.
Practice
(1/5)
1. What does the groupBy operation do in Kafka Streams?
easy
A. It sends data to an external database.
B. It deletes records from the stream.
C. It organizes records by a new key to prepare for aggregation.
D. It filters records based on a condition.
Solution
Step 1: Understand the purpose of groupBy
The groupBy operation changes the key of records to group them logically.
Step 2: Connect grouping to aggregation
Grouping is necessary before aggregation to compute summaries per group.
Final Answer:
It organizes records by a new key to prepare for aggregation. -> Option C
Quick Check:
groupBy = organize by new key [OK]
Hint: GroupBy changes keys to group data before aggregation [OK]
Common Mistakes:
Thinking groupBy deletes data
Confusing groupBy with filtering
Assuming groupBy sends data outside Kafka
2. Which of the following is the correct syntax to perform a count aggregation after grouping a Kafka stream named orders by customerId?
easy
A. orders.count().groupBy(value -> value.customerId);
B. orders.groupBy((key, value) -> value.customerId).count();
C. orders.groupByKey().sum();
D. orders.aggregate((key, value) -> value.customerId).count();
Solution
Step 1: Identify correct groupBy syntax
The groupBy method takes a key selector function like (key, value) -> value.customerId.
Step 2: Apply count aggregation after grouping
Calling count() after groupBy correctly counts records per group.
Final Answer:
orders.groupBy((key, value) -> value.customerId).count(); -> Option B
Quick Check:
groupBy then count = correct syntax [OK]
Hint: Group first, then aggregate like count() [OK]
Common Mistakes:
Calling count() before groupBy
Using groupByKey() without key selector
Mixing aggregate() with wrong parameters
3. Given the following Kafka Streams code snippet, what is the output type of customerCounts?
var customerCounts = orders
.groupBy((key, order) -> order.customerId)
.count();
medium
A. KTable<String, Long>
B. KStream<String, Integer>
C. KStream<String, Long>
D. KTable<String, String>
Solution
Step 1: Understand groupBy and count return types
Grouping returns a grouped stream, and count() returns a KTable with counts per key.
Step 2: Identify key and value types
The key is String (customerId), and count returns Long values.
Final Answer:
KTable<String, Long> -> Option A
Quick Check:
count() returns KTable with Long values [OK]
Hint: count() after groupBy returns KTable with Long values [OK]
Common Mistakes:
Assuming count returns KStream
Confusing Long with Integer type
Mixing up KStream and KTable
4. Identify the error in this Kafka Streams code snippet:
var result = orders
.groupBy(order -> order.customerId)
.count();
medium
A. Missing a filter() before groupBy.
B. count() cannot be called after groupBy.
C. The variable 'orders' must be a KTable, not a KStream.
D. groupBy requires a key-value mapper, not a single argument.
Solution
Step 1: Check groupBy method signature
groupBy expects a function with two parameters: key and value, e.g., (key, value) -> newKey.
Step 2: Identify incorrect lambda usage
The code uses a single-argument lambda order -> order.customerId, which is invalid for groupBy.
Final Answer:
groupBy requires a key-value mapper, not a single argument. -> Option D
Quick Check:
groupBy needs (key, value) -> newKey [OK]
Hint: groupBy needs two parameters in lambda [OK]
Common Mistakes:
Using single-argument lambda for groupBy
Thinking count() is invalid after groupBy
Assuming orders must be KTable
5. You want to compute the total sales amount per product category from a Kafka stream sales where each record has category and amount. Which Kafka Streams code snippet correctly performs this aggregation?