HomeLinuxApache Kafka Shopper Instance from CLI

Apache Kafka Shopper Instance from CLI


The Kafka Shopper CLI software is a command-line interface that enables us to subscribe to a number of Kafka subjects and eat the messages from them. It’s a simplistic however highly effective command-line utility that turns out to be useful when debugging or testing an software for growth functions.

Kafka Console Shopper Device

We will entry the Kafka shopper CLI software utilizing the Kafka-console-consumer.sh utility. This software is a part of the Apache Kafka distribution and is applied in Java. The Kafka Shopper API gives a high-level, abstracted interface to eat the data from a Kafka cluster.

Utilizing the CLI interface, the software can carry out as a shopper shopper which permits us to attach a specified Kafka dealer and fetch the messages from the desired subjects.

It then shows the messages from the subject within the terminal output, offering a straightforward option to view and examine the info which might be produced by producers.

This tutorial explores arrange a fundamental Kafka shopper shopper utilizing the Kafka Shopper CLI interface.

Create a Pattern Subject in Kafka

Allow us to begin by making a pattern matter on which we subscribe and skim the messages. We use the kafka-topics.sh utility.

For instance, suppose we wish to create a brand new matter known as “coordinates”. We will run the command as proven:

./bin/kafka-topics.sh –create –topic coordinates –replication-factor 3 –partitions 3 –bootstrap-server localhost:9092

Within the given instance, we use the –create choice to arrange a brand new matter known as “coordinates”. We then specify the replication issue of three. Which means that every matter partition is replicated throughout 3 Kafka brokers.

Lastly, we specify the variety of partitions as 3. Which means that the subject is split into three partitions.

Produce Messages in Kafka

As soon as we created the subject, we will write to it utilizing the kafka-console-producer command. An instance is as follows:

./bin/kafka-console-producer.sh –topic coordinates –bootstrap-server localhost:9092 << EOF
> {“latitude”: 37.7749, “longitude”: –122.4194}
> {“latitude”: 40.7128, “longitude”: –74.0060}
> {“latitude”: 51.5074, “longitude”: –0.1278}
> {“latitude”: 35.6895, “longitude”: 139.6917}
> {“latitude”: 55.7558, “longitude”: 37.6173}
> {“latitude”: –33.8651, “longitude”: 151.2093}
> {“latitude”: 48.8566, “longitude”: 2.3522}
> {“latitude”: 37.5665, “longitude”: 126.9780}
> {“latitude”: 35.6762, “longitude”: 139.6503}
> {“latitude”: 43.6532, “longitude”: –79.3832}
> EOF

Within the earlier command, we use the kafka-console-producer.sh utility to provide the messages to the “coordinates” matter. The –matter parameter lets us specify which matter we want to write.

Eat Messages in Kafka

As you possibly can guess, we will eat the messages from the “coordinates” matter utilizing the kafka-console-consumer software as proven within the following:

./bin/kafka-console-consumer.sh –topic coordinates –bootstrap-server localhost:9092 –from-beginning

On this case, the command ought to learn the messages from the coordinates from the start as decided by the –from-beginning parameter.

Instance Output:

Conclusion

You now discovered use the Kafka CLI instruments to create a brand new matter, produce messages, subscribe to a Kafka matter, and skim messages from it.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments