0
0
Kafkadevops~10 mins

Filter and map operations in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Filter and map operations
Start with stream of messages
Apply filter condition
Keep message
Apply map function
Output transformed messages
Messages flow through a filter to keep only those matching a condition, then a map transforms each kept message.
Execution Sample
Kafka
stream.filter((key, value) -> value.toLowerCase().contains("error"))
      .mapValues(value -> value.toUpperCase())
      .foreach((key, value) -> System.out.println(value));
This code filters messages containing 'error' and converts their values to uppercase before printing.
Process Table
StepMessage ValueFilter Condition (contains 'error')Filter ResultMap Operation (toUpperCase)Output
1System startedfalseDiscard--
2Error detectedtrueKeepERROR DETECTEDERROR DETECTED
3User loginfalseDiscard--
4Critical errortrueKeepCRITICAL ERRORCRITICAL ERROR
5ShutdownfalseDiscard--
💡 All messages processed; only those with 'error' passed filter and were mapped.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Current Message-System startedError detectedUser loginCritical errorShutdown-
Filter Result-falsetruefalsetruefalse-
Mapped Value--ERROR DETECTED-CRITICAL ERROR--
Output--ERROR DETECTED-CRITICAL ERROR--
Key Moments - 2 Insights
Why are some messages not transformed or printed?
Messages failing the filter condition are discarded early (see rows 1,3,5 in execution_table), so map and output steps do not run for them.
Does the map operation change the original message?
No, map creates a new transformed value (uppercase string) without modifying the original message, as shown in the 'Mapped Value' column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 4?
ACritical error
BCRITICAL ERROR
Ccritical error
D-
💡 Hint
Check the 'Output' column at step 4 in the execution_table.
At which step does the filter condition become false for the first time?
AStep 3
BStep 2
CStep 1
DStep 5
💡 Hint
Look at the 'Filter Result' column in execution_table for the earliest 'false'.
If the filter condition changed to check for 'warning' instead of 'error', which message would be kept at step 2?
A-
BSystem started
CUser login
DError detected
💡 Hint
Refer to the original message values and see if 'warning' appears in step 2 message.
Concept Snapshot
Filter and map in Kafka streams:
- filter(condition): keeps messages matching condition
- map(function): transforms each kept message
- Order matters: filter first, then map
- Messages failing filter are dropped
- Map creates new transformed output
Full Transcript
In Kafka streams, messages flow through operations like filter and map. First, filter checks each message's value to keep only those containing 'error'. Messages not matching are discarded immediately. Then, map transforms each kept message by converting its value to uppercase. The output shows only transformed messages that passed the filter. This step-by-step trace shows how messages are processed, filtered, transformed, and output. Understanding this flow helps beginners see why some messages are dropped and how transformations apply only after filtering.