Consider a Kafka Streams application that processes a stream of integers. It filters out even numbers and then maps each remaining number to its square. What will be the output for the input stream [1, 2, 3, 4, 5]?
KStream<Integer, Integer> input = builder.stream("input-topic"); KStream<Integer, Integer> processed = input .filter((key, value) -> value % 2 != 0) .mapValues(value -> value * value); processed.to("output-topic");
Remember that filter removes even numbers, and mapValues squares the remaining numbers.
The filter keeps only odd numbers: 1, 3, 5. Then mapValues squares each: 1*1=1, 3*3=9, 5*5=25. So the output is [1, 9, 25].
Given a Kafka Streams pipeline that filters records with keys less than 3 and maps values by adding 10, what is the output for input records with keys and values: (1, 5), (2, 7), (3, 9), (4, 11)?
KStream<Integer, Integer> input = builder.stream("input-topic"); KStream<Integer, Integer> result = input .filter((key, value) -> key < 3) .mapValues(value -> value + 10); result.to("output-topic");
Filter keeps keys less than 3, then values get 10 added.
Only records with keys 1 and 2 pass the filter. Their values 5 and 7 become 15 and 17 after adding 10.
Examine the following Kafka Streams code snippet. It attempts to filter and map values but causes a runtime NullPointerException. What is the cause?
KStream<Integer, String> input = builder.stream("input-topic"); KStream<Integer, String> result = input .filter((key, value) -> value.length() > 3) .mapValues(value -> value.toUpperCase()); result.to("output-topic");
Think about what happens if value is null when calling value.length().
If any record has a null value, calling value.length() causes a NullPointerException at runtime.
Choose the syntactically correct Kafka Streams code that filters values greater than 10 and maps values to their double.
Check the syntax of lambda expressions in Java.
Option C uses correct Java lambda syntax. Option C misses parentheses around parameters. Option C uses JavaScript arrow syntax. Option C uses braces without return statement causing no boolean result.
A Kafka Streams application reads from a topic with 6 records having values: [5, 12, 7, 20, 3, 15]. It filters values greater than 10 and then maps values to value minus 5. How many records are in the output topic?
KStream<Integer, Integer> input = builder.stream("input-topic"); KStream<Integer, Integer> output = input .filter((k, v) -> v > 10) .mapValues(v -> v - 5); output.to("output-topic");
Count how many values are greater than 10 before mapping.
Values greater than 10 are 12, 20, and 15. So 3 records pass the filter and appear in output.