Bird
0
0

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

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

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

    producer.send(new ProducerRecord<>(topic, key, value)); matches this order exactly, others mix parameters incorrectly.
  3. Final Answer:

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

    Topic, key, value order = B [OK]
Quick Trick: Remember ProducerRecord(topic, key, value) order [OK]
Common Mistakes:
  • Swapping key and value positions
  • Omitting the key parameter
  • Placing key before topic incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes