kafka-console-consumer
tool.
Before we start using the CLI, make sure you have started Kafka beforehand.
Use CLI commands with appropriate extensions for your platform: for Windows -
kafka-console-consumer.bat
, for Mac and Linux - kafka-console-consumer.sh
.--group
flag.
How to create consumers in a Kafka consumer group?
To start consumers in a consumer group, do the following:- Create a topic with at least 2 partitions and send data to it
- Create a first
kafka-console-consumer
and assign a group name with--group
- Open a new terminal / shell window
- Create a second
kafka-console-consumer
and use the same--group
argument - Send data to the topic and you will see consumers sharing the reads
Create consumer group example
You cannot have more consumers in a group than partitions in your Kafka topic, and therefore we first need to create a Kafka topic with a few partitions (in the example 3).my-first-application
my-first-application
(note we’re using the exact same command)
my-first-application
my-first-application
will get assigned a partition. Produce a few string messages in the topic.
Stop all consumers
And keep on producing to the topicGotchas
-
If you consume in a consumer groups using the
--group
command, then if you try using the--from-beginning
option afterwards with the same group, it will be ignored. Instead, you need to reset your consumer groups as shown here. - If you don’t specify a —group option, the consumer group of the consumer will be a random consumer group such as console-consumer-11984
-
If you see one consumer getting all the messages, that probably means that your topic was only created with 1 partition, which you can verify with the
kafka-topics --describe
command