Consider the following Java Kafka producer code snippet. 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"); KafkaProducer<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 only one partition exists.
The code sends a message to the topic "test-topic". If the topic has only one partition (partition 0), the message will be sent there. The code prints the partition number, so the output is "Sent to partition: 0".
When configuring a Kafka producer in Java, which property key must be set to specify the Kafka broker address?
It usually contains the word 'bootstrap'.
The correct property to specify Kafka broker addresses is bootstrap.servers. It tells the producer where to connect initially.
Examine the following Java 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 KafkaProducer<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 producer requires both key.serializer and value.serializer properties. Missing value.serializer causes a KafkaException indicating the serializer is missing.
Identify the option that fixes the syntax error in the following Java Kafka producer snippet:
KafkaProducerproducer = new KafkaProducer(props); ProducerRecord record = new ProducerRecord<>("topic", "key", "value") producer.send(record); producer.close();
Check missing semicolons and proper generic usage.
The ProducerRecord line is missing a semicolon. Also, the KafkaProducer constructor should use diamond operator <> for type safety. Option B fixes both issues.
Given the following Java 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"); try (KafkaProducer<String, String> producer = new KafkaProducer<>(props)) { for (int i = 0; i < 5; i++) { ProducerRecord<String, String> record = new ProducerRecord<>("my-topic", "key" + i, "value" + i); producer.send(record); if (i == 2) { break; } } }
Look at the loop and the break statement carefully.
The loop runs from 0 to 4 but breaks when i == 2, so it sends messages for i = 0, 1, 2 only, totaling 3 messages.