Bird
0
0

Which of the following is the correct way to specify a key when producing a Kafka message in Java?

easy📝 Syntax Q3 of 15
Kafka - Topics and Partitions
Which of the following is the correct way to specify a key when producing a Kafka message in Java?
Aproducer.send(new ProducerRecord<>(topic, key, value));
Bproducer.send(new ProducerRecord<>(topic, value));
Cproducer.send(new ProducerRecord<>(key, topic, value));
Dproducer.send(new ProducerRecord<>(value, key));
Step-by-Step Solution
Solution:
  1. Step 1: Recall ProducerRecord constructor

    The correct constructor is ProducerRecord(topic, key, value).
  2. Step 2: Match options to constructor

    producer.send(new ProducerRecord<>(topic, key, value)); matches the correct order: topic, key, value.
  3. Final Answer:

    producer.send(new ProducerRecord<>(topic, key, value)); -> Option A
  4. Quick Check:

    Correct ProducerRecord syntax = C [OK]
Quick Trick: Topic first, then key, then value in ProducerRecord [OK]
Common Mistakes:
  • Swapping key and value positions
  • Omitting the key when needed
  • Passing parameters in wrong order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes