Kafka Consumer Examples Using Java: Apache Kafka is an open-source stream-processing software platform developed by LinkedIn and donated to the Apache Software Foundation, written in Scala and Java.

What is Kafka Consumer?

The consumer is used for the consumption of Kafka data inserted by Kafka Producer. The primary role of a Kafka consumer is to take Kafka connection and consumer properties to read records from the respective Kafka broker.

In this tutorial, we will see how to create a Kafka Consumer examples using java.

Create the maven project and add below dependencies to your POM file

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka_2.12</artifactId>
    <version>2.3.1</version>
</dependency>

Then Create Package and Create a class with Name SimpleConsumer.java

use below code

package org.kafka.pratice;

import java.util.Arrays;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;

public class SimpleConsumer {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Properties props = new Properties();
		props.put("bootstrap.servers", "localhost:9092");
		props.put("group.id", "test");
		props.put("enable.auto.commit", "false");
		props.put("auto.commit.interval.ms", "1000");
		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<String, String>(props);
		// assigning partition0 to topic
		TopicPartition partition0 = new TopicPartition("testTopic", 0);
		consumer.assign(Arrays.asList(partition0));
		consumer.seekToBeginning(Arrays.asList(partition0));

		while (true) {
			ConsumerRecords<String, String> records = consumer.poll(100);
			for (ConsumerRecord<String, String> record : records) {
				System.out.println("offset = " + record.offset() + ",key = " + record.key() + " value = " + record.value());
			}
		}
	}

}

In the above code, records are displayed from offset Zero to modify the code to read records from current offset then use below code

consumer.subscribe(Arrays.asList("testTopic"));
		while (true) {
			ConsumerRecords&lt;String, String&gt; records = consumer.poll(100);
			for (ConsumerRecord&lt;String, String&gt; record : records) {
				System.out.println("offset = " + record.offset() + ",key = " + record.key() + " value = " + record.value());
			}
		}

the above code will work for getting the latest records from Kafka broker.

Run the above code to get all records or latest records from the Kafka server.

Thanks

Also Read: Kafka Producer Examples Using Java

Kafka Consumer Examples Using Java

Leave a Reply

Your email address will not be published. Required fields are marked *