Bird
Raised Fist0

Identify the error in this Kafka Streams code snippet:

medium📝 Debug Q14 of Q15
Kafka - Streams
Identify the error in this Kafka Streams code snippet:
var result = orders
  .groupBy(order -> order.customerId)
  .count();
AMissing a filter() before groupBy.
Bcount() cannot be called after groupBy.
CThe variable 'orders' must be a KTable, not a KStream.
DgroupBy requires a key-value mapper, not a single argument.
Step-by-Step Solution
Solution:
  1. Step 1: Check groupBy method signature

    groupBy expects a function with two parameters: key and value, e.g., (key, value) -> newKey.
  2. Step 2: Identify incorrect lambda usage

    The code uses a single-argument lambda order -> order.customerId, which is invalid for groupBy.
  3. Final Answer:

    groupBy requires a key-value mapper, not a single argument. -> Option D
  4. Quick Check:

    groupBy needs (key, value) -> newKey [OK]
Quick Trick: groupBy needs two parameters in lambda [OK]
Common Mistakes:
MISTAKES
  • Using single-argument lambda for groupBy
  • Thinking count() is invalid after groupBy
  • Assuming orders must be KTable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes