📖 Scenario: You are working with a stream of messages representing orders in a Kafka topic. Each message contains an order ID and the order amount. You want to process this stream to find only the orders with amounts greater than 100 and then create a new stream with just the order IDs of these large orders.
🎯 Goal: Build a Kafka Streams application that filters orders with amounts greater than 100 and maps them to their order IDs.
📋 What You'll Learn
Create a KStream named orders with sample order data
Create a variable threshold set to 100
Use filter on orders to keep only orders with amount greater than threshold
Use map to transform filtered orders to just their order IDs
Print the resulting stream to the console
💡 Why This Matters
🌍 Real World
Filtering and mapping streams is common in real-time data processing, such as monitoring orders, transactions, or sensor data.
💼 Career
Kafka Streams skills are valuable for backend developers, data engineers, and anyone working with real-time data pipelines.
Progress0 / 4 steps
1
Create the initial orders stream
Create a KStream called orders with these exact entries: ("order1", 50), ("order2", 150), ("order3", 200), ("order4", 80).
Kafka
Hint
Use builder.stream("orders-topic") to create the stream. The actual data is assumed to be in the topic.
2
Set the threshold value
Create an integer variable called threshold and set it to 100.
Kafka
Hint
Just create a simple integer variable named threshold and assign 100.
3
Filter and map the orders stream
Use filter on orders to keep only entries where the value (order amount) is greater than threshold. Then use map to transform the filtered stream to only the order IDs (the keys). Store the result in a KStream called largeOrderIds.
Kafka
Hint
Use filter with a lambda checking amount > threshold. Then use map with a lambda returning KeyValue.pair(orderId, orderId).
4
Print the filtered order IDs
Use foreach on largeOrderIds to print each order ID with the exact format: "Large order ID: <orderId>".
Kafka
Hint
Use foreach with a lambda that prints the string exactly as shown.
Practice
(1/5)
1. What does the filter operation do in Kafka Streams?
easy
A. Deletes all messages in the stream
B. Changes messages to a new form
C. Keeps only messages that meet a condition
D. Sends messages to a different topic
Solution
Step 1: Understand filter purpose
The filter operation selects messages that satisfy a given condition, removing others.
Step 2: Compare with other operations
Unlike map, which changes message content, filter only keeps or drops messages.
Final Answer:
Keeps only messages that meet a condition -> Option C
Quick Check:
Filter = Keep messages by condition [OK]
Hint: Filter keeps or drops messages based on a rule [OK]
Common Mistakes:
Confusing filter with map operation
Thinking filter changes message content
Assuming filter deletes all messages
2. Which of the following is the correct syntax to apply a map operation on a Kafka stream named stream to convert messages to uppercase?
easy
A. stream.map(value.toUpperCase)
B. stream.filter(value -> value.toUpperCase())
C. stream.map(value => value.toUpperCase())
D. stream.map(value -> value.toUpperCase())
Solution
Step 1: Identify correct lambda syntax in Kafka Streams
Kafka Streams in Java uses map(value -> ...) syntax for mapping values.
Step 2: Check method call correctness
value.toUpperCase() correctly converts string to uppercase.
Final Answer:
stream.map(value -> value.toUpperCase()) -> Option D
Quick Check:
Correct lambda and method call = stream.map(value -> value.toUpperCase()) [OK]
Hint: Use arrow -> for lambda in Kafka Streams Java [OK]
Common Mistakes:
Using filter instead of map
Using JavaScript arrow => instead of Java ->
Missing parentheses in method call
3. Given the following Kafka Streams code, what will be the output messages after processing?
C. The second filter uses the mapped uppercase value correctly
D. The lambda syntax is incorrect
Solution
Step 1: Check filter and map chaining
Chaining multiple filters and maps is allowed in Kafka Streams.
Step 2: Verify lambda syntax and logic
Lambdas use correct syntax and logic: first filter checks start, map uppercases, second filter checks length.
Final Answer:
No error; code is correct -> Option A
Quick Check:
Multiple filters and maps allowed = No error; code is correct [OK]
Hint: Multiple filters and maps can be chained without error [OK]
Common Mistakes:
Thinking multiple filters are disallowed
Confusing lambda syntax with other languages
Assuming map changes variable type incorrectly
5. You want to process a Kafka stream of user names to keep only those starting with 'J' and convert them to lowercase. Which code snippet correctly does this?
hard
A. stream.filter(name -> name.startsWith("J"))
.map(name -> name.toLowerCase())
B. stream.map(name -> name.toLowerCase())
.filter(name -> name.startsWith("J"))
C. stream.filter(name -> name.toLowerCase().startsWith("j"))
.map(name -> name.toUpperCase())
D. stream.map(name -> name.startsWith("J"))
.filter(name -> name.toLowerCase())
Solution
Step 1: Filter names starting with 'J'
Use filter(name -> name.startsWith("J")) to keep only names starting with uppercase 'J'.
Step 2: Map filtered names to lowercase
Apply map(name -> name.toLowerCase()) to convert names to lowercase.
Final Answer:
stream.filter(name -> name.startsWith("J"))
.map(name -> name.toLowerCase()) -> Option A
Quick Check:
Filter first, then map to lowercase [OK]
Hint: Filter first by condition, then map to transform [OK]
Common Mistakes:
Mapping before filtering causing wrong results
Using incorrect lambda syntax
Filtering after changing case causing misses
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.