Consider the following Kafka Producer code in Java. What will be printed to the console?
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Producer<String, String> producer = new KafkaProducer<>(props); ProducerRecord<String, String> record = new ProducerRecord<>("test-topic", "key1", "value1"); try { RecordMetadata metadata = producer.send(record).get(); System.out.println("Sent to partition: " + metadata.partition()); } catch (Exception e) { System.out.println("Send failed"); } producer.close();
By default, Kafka assigns messages to partition 0 if no custom partitioner is set and the topic has one partition.
The code sends a message to the topic "test-topic" which by default has one partition (partition 0). The send is synchronous using get(), so the metadata returned shows partition 0.
In Kafka Producer configuration, which property determines how many acknowledgments the producer requires the leader to receive before considering a request complete?
Think about the property that controls message durability guarantees.
The acks property controls the number of acknowledgments the leader broker must receive before the producer considers the send successful. Other options control retries, batch size, and delay.
Examine the following Kafka Producer code snippet. What error will it raise when run?
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); // Missing value.serializer property Producer<String, String> producer = new KafkaProducer<>(props); ProducerRecord<String, String> record = new ProducerRecord<>("test-topic", "key1", "value1"); producer.send(record); producer.close();
Check if all required serializer properties are set.
The value.serializer property is missing, so Kafka cannot create the producer properly and throws a KafkaException during construction.
Choose the correct Java code snippet that creates a Kafka Producer with String key and value serializers.
The serializer properties require the full class name as a string.
The correct way is to provide the full class name as a string. Options B and C use wrong types, and D uses incomplete class names causing errors.
Given the following Kafka Producer code, how many messages will be sent to the topic?
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Producer<String, String> producer = new KafkaProducer<>(props); for (int i = 0; i < 5; i++) { ProducerRecord<String, String> record = new ProducerRecord<>("test-topic", "key" + i, "value" + i); producer.send(record); if (i == 2) { break; } } producer.close();
Consider the effect of the break statement inside the loop.
The loop runs from i=0 to i=4 but breaks when i==2, so it sends messages for i=0,1,2 only, totaling 3 messages.