Bird
0
0

Given the following Kafka Streams processor code snippet, what will be the output stored in the state store after processing the input key-value pairs ("key1", 1) and ("key1", 2)?

medium📝 Predict Output Q13 of 15
Kafka - Advanced Stream Processing
Given the following Kafka Streams processor code snippet, what will be the output stored in the state store after processing the input key-value pairs ("key1", 1) and ("key1", 2)?
KeyValueStore<String, Integer> store = context.getStateStore("countStore");
Integer current = store.get("key1");
if (current == null) current = 0;
store.put("key1", current + value);
AThe state store will have key "key1" with value 1
BThe state store will have key "key1" with value 2
CThe state store will have key "key1" with value 3
DThe state store will be empty
Step-by-Step Solution
Solution:
  1. Step 1: Trace code execution for both inputs

    First ("key1", 1): store.get("key1") returns null, current = 0, store.put("key1", 0 + 1) = 1. Second ("key1", 2): store.get("key1") = 1, current = 1, store.put("key1", 1 + 2) = 3.
  2. Final Answer:

    The state store will have key "key1" with value 3 -> Option C
  3. Quick Check:

    Sum values for key1 = 3 [OK]
Quick Trick: Add new value to existing store value for same key [OK]
Common Mistakes:
  • Assuming store resets between messages
  • Not handling null initial value
  • Overwriting instead of adding values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes