Properties object with Kafka consumer settingsKafkaConsumer<String, String> object using the propertiestest-topicProperties object with Kafka consumer settingsKafkaConsumer<String, String> object using the propertiestest-topicProperties object called props and set these exact properties:bootstrap.servers to "localhost:9092",group.id to "test-group",key.deserializer to "org.apache.kafka.common.serialization.StringDeserializer",value.deserializer to "org.apache.kafka.common.serialization.StringDeserializer".Use props.put(key, value) to set each property.
KafkaConsumer<String, String> called consumer using props. Then subscribe consumer to the topic "test-topic" using consumer.subscribe().Use Collections.singletonList("test-topic") to subscribe to one topic.
consumer.poll() with a timeout of 1000 milliseconds to get records. Then use a for loop with variables record to iterate over records. Inside the loop, print the message key and value using record.key() and record.value().Use Duration.ofMillis(1000) for the poll timeout.
consumer.close() to close the consumer. Then print "Finished reading messages" using System.out.println().Remember to close the consumer to free resources.
What is the main purpose of a Java consumer client in Kafka?
Which of the following is the correct way to subscribe a Kafka consumer to a topic named my-topic?
consumer._____ (Collections.singletonList("my-topic"));subscribe method registers the consumer to listen to topics.consumer.subscribe(Collections.singletonList("my-topic")) correctly subscribes to the topic.What will the following Java Kafka consumer code print if the topic has one message with value "Hello Kafka"?
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
System.out.println(record.value());
}poll method fetches available messages; here it gets one message.value() of each record, which is "Hello Kafka".Identify the error in this Kafka consumer code snippet:
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "group1");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("my-topic"));
ConsumerRecords<String, String> records = consumer.poll(100);
for (ConsumerRecord<String, String> record : records) {
System.out.println(record.value());
}poll requires a Duration argument, not an int.poll(100) (int), should be poll(Duration.ofMillis(100)).You want to consume messages from two topics topicA and topicB and print their keys and values. Which code snippet correctly subscribes and processes messages?
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "group1");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer._____ (Arrays.asList("topicA", "topicB"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
System.out.println("Key: " + record.key() + ", Value: " + record.value());
}
}subscribe accepts a list of topics to listen to, while assign assigns specific partitions.consumer.subscribe(Arrays.asList("topicA", "topicB")) subscribes to both topics properly.Pass the test and this pattern moves unknown