Integration / Apache Kafka Interview questions
What is a Kafka consumer?
A consumer is the client application that subscribes to one or more topics and reads records from them, tracking its own position (offset) in each partition it's assigned so it knows what to read next.
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("group.id", "order-processors"); 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(List.of("orders")); while (true) { ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord<String, String> record : records) { System.out.println(record.value()); } }
Unlike many traditional queue consumers, a Kafka consumer pulls data by repeatedly calling poll() rather than having records pushed to it, which lets the consumer control its own processing pace rather than being overwhelmed by an inbound push.
More Related questions...