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:
Step 1: Match key serializer with key type
Integer keys require IntegerSerializer.
Step 2: Match value serializer with value type
String values require StringSerializer.
Step 3: Confirm generic types in KafkaProducer
KafkaProducer matches the serializers.
Final Answer:
Option A correctly configures serializers and producer declaration. -> Option A
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
Master "with Java/Python" in Kafka
9 interactive learning modes - each teaches the same concept differently