Senior
Do you read messages from Kafka one by one or in batches?
sobes.tech AI
Answer from AI
In Java, when working with Kafka, messages are usually read in batches rather than one by one.
This is because Kafka is optimized for processing data streams, and reading in batches improves performance and reduces network call overhead.
Example of reading messages in batches using KafkaConsumer:
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("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());
}
}
Here, poll() returns a set of messages (a batch) at once, which allows for efficient data processing.