Bird
Raised Fist0

You need to produce messages with Integer keys and String values using KafkaProducer in Java. Which of the following configurations and declarations is correct?

hard🚀 Application Q8 of Q15
Kafka - with Java/Python
You need to produce messages with Integer keys and String values using KafkaProducer in Java. Which of the following configurations and declarations is correct?
Aprops.put("key.serializer", "org.apache.kafka.common.serialization.IntegerSerializer");<br>props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");<br>KafkaProducer<Integer, String> producer = new KafkaProducer<>(props);
Bprops.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");<br>props.put("value.serializer", "org.apache.kafka.common.serialization.IntegerSerializer");<br>KafkaProducer<String, Integer> producer = new KafkaProducer<>(props);
Cprops.put("key.serializer", "org.apache.kafka.common.serialization.LongSerializer");<br>props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");<br>KafkaProducer<Long, String> producer = new KafkaProducer<>(props);
Dprops.put("key.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");<br>props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");<br>KafkaProducer<byte[], String> producer = new KafkaProducer<>(props);
Step-by-Step Solution
Solution:
  1. Step 1: Match key serializer with key type

    Integer keys require IntegerSerializer.
  2. Step 2: Match value serializer with value type

    String values require StringSerializer.
  3. Step 3: Confirm generic types in KafkaProducer

    KafkaProducer matches the serializers.
  4. Final Answer:

    Option A correctly configures serializers and producer declaration. -> Option A
  5. Quick Check:

    Serializer must match key/value types exactly. [OK]
Quick Trick: Serializer class must match key/value data types. [OK]
Common Mistakes:
MISTAKES
  • Swapping key and value serializers
  • Using wrong serializer for Integer keys
  • Mismatching generic types and serializers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes