Junior — Middle
Have you used Kafka for implementing consumer-side interactions?
sobes.tech AI
Answer from AI
Yes, I have used Kafka as a consumer to receive messages from topics. Typically, a Kafka Consumer is configured on the consumer side, which subscribes to the necessary topics and processes incoming messages in real-time.
Example in Java using Kafka Consumer API:
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
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(Arrays.asList("my-topic"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
// message processing logic here
}
}
This approach allows asynchronous and reliable receipt and processing of data from event streams.