Bird
Raised Fist0

You want to process messages from a Kafka topic and filter out any messages with empty values before printing. Which code snippet correctly implements this using the Java consumer client?

hard🚀 Application Q9 of Q15
Kafka - with Java/Python

You want to process messages from a Kafka topic and filter out any messages with empty values before printing. Which code snippet correctly implements this using the Java consumer client?

consumer.subscribe(Collections.singletonList("my-topic"));
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
for (ConsumerRecord<String, String> record : records) {
    // What to add here?
}
Aif (record.value() == null) { System.out.println(record.value()); }
BSystem.out.println(record.value());
Cif (record.value().length() == 0) { System.out.println(record.value()); }
Dif (!record.value().isEmpty()) { System.out.println(record.value()); }
Step-by-Step Solution
Solution:
  1. Step 1: Filter out empty strings

    Check if the message value is not empty using !record.value().isEmpty() before printing.
  2. Step 2: Eliminate incorrect conditions

    Checking for null prints null values, checking length == 0 prints empty strings, and printing without condition prints all messages.
  3. Final Answer:

    if (!record.value().isEmpty()) { System.out.println(record.value()); } -> Option D
  4. Quick Check:

    Filter empty values with isEmpty() check = B [OK]
Quick Trick: Use !value.isEmpty() to skip empty messages [OK]
Common Mistakes:
MISTAKES
  • Checking null instead of empty string
  • Printing all messages without filtering
  • Using length()==0 condition incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes