#
JAVA: Create Kafka Consumer
This tutorial explains to you how to create a Kafka consumer using Java. This tutorial has an example as well.
A nice job to do is to read messages from a Kafka topic. In this tutorial, I will explain to you how to read a message from a Kafka topic in Java.
First of all you must install a Kafka server in order to test it. For this example you don't need to install a Kafka cluster.
So, on my environment you have some prerequisites in order to test the Java code below.
PREREQUISITES:
From the machine you run the Java client you must see the private Kafka server sockets (hostname:IP)
You must have a Kafka topic named my-topic10
-- List all topics (prints only topic names)
kafka-topics.sh --list --zookeeper zookeeper1:2181/kafka
And now look at the code.
In pom.xml we must have :
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.8.0</version>
</dependency>
And the class which create the Kafka consumer is like this :
Info
In the partitions, each of the messages will have a specific ID attached to it, called the Offset
.
Info
Arrays.asList(topic)
method doesn't copy the elements from the array to the new List object.
The method provides a List view on the given array (all changes in the array will be visible in the returned list).
Info
The poll() method provides a timeout argument. This is the maximum amount of time to listen for messages before returning an empty list of records.