Bird
Raised Fist0

What is wrong with this Kafka Streams aggregation code?

medium📝 Debug Q7 of Q15
Kafka - Streams
What is wrong with this Kafka Streams aggregation code?
var aggregated = stream.groupBy((k, v) -> v.getCategory()).aggregate(
  () -> 0,
  (key, value, aggregate) -> aggregate + value.getAmount()
);
Aaggregate() requires a Materialized parameter
BInitial value must be a Long, not int
CgroupBy() cannot use lambda expressions
Daggregate() cannot sum values
Step-by-Step Solution
Solution:
  1. Step 1: Check aggregate() method requirements

    The aggregate() method requires a Materialized parameter to specify state store details.
  2. Step 2: Identify missing Materialized parameter

    In the code, Materialized is missing, which will cause a compilation error.
  3. Final Answer:

    aggregate() requires a Materialized parameter -> Option A
  4. Quick Check:

    aggregate() needs Materialized for state store [OK]
Quick Trick: Always provide Materialized when using aggregate() [OK]
Common Mistakes:
MISTAKES
  • Omitting Materialized parameter
  • Confusing initial value type
  • Thinking groupBy disallows lambdas

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes