Kafka - Consumer Groups CLI
đĨ
Refer back to the theories section for more information on consumer groups.
Description đ
Used to divide partitions
among consumers
.
Basic Commands
đ
-
start a
consumer
in aconsumer group
kafka-console-consumer.sh --bootstrap-server <kafka-broker> --topic <topic-name> --group <group-name>
-
describe a
consumer group
kafka-consumer-groups.sh --bootstrap-server <kafka-broker> --group <group-name> --describe
-
reset a
consumer group
toearliest
offset to the earliest offsetkafka-consumer-groups.sh --bootstrap-server <kafka-broker> --group <group-name> --reset-offsets --to-earliest --execute --topic <topic-name>
- the
--topic
flag is needed if theconsumer group
is consuming from multipletopics
- you can use
--all-topics
instead of--topic
to reset alltopics
in theconsumer group
- you can use
- the
--execute
flag is required to actually reset the offsets
- the
-
shift the
consumer group
offset byn
messageskafka-consumer-groups.sh --bootstrap-server <kafka-broker> --group <group-name> --reset-offsets --shift-by <number-of-messages> --execute --topic <topic-name>
shift-by
can be a negative number to shift the offset backwards byn
messages
Examples đ§Š
- create a
topic
with 3partitions
and 1replication factor
-
its necessary to have at least 2
partitions
to make use of aconsumer group
kafka-topics.sh --bootstrap-server localhost:9092 --create --topic first_topic --partitions 3 --replication-factor 1
-
-
start a
consumer
in theconsumer group
my-first-applicationkafka-console-consumer.sh --bootstrap-server localhost:9092 --topic first_topic --group my-first-application
-
start a
producer
and start producingmessages
to thetopic
kafka-console-producer.sh --bootstrap-server localhost:9092 --topic first_topic
-
-
start another
consumer
part of the samegroup
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic first_topic --group my-first-application
-
start another
consumer
part of a differentgroup
from beginning, i.e. my-second-applicationkafka-console-consumer.sh --bootstrap-server localhost:9092 --topic first_topic --group my-second-application --from-beginning
-
observe the messages being spread
-
describe the
consumer group
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group my-first-application --describe
- output
đ kafka-zero-to-pro ⯠kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group my-first-group GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID my-first-group first_topic 0 5 5 0 console-consumer-5d028f33-0549-4826-a719-70b6188d5f52 /127.0.0.1 console-consumer my-first-group first_topic 1 2 2 0 console-consumer-5d028f33-0549-4826-a719-70b6188d5f52 /127.0.0.1 console-consumer my-first-group first_topic 2 1 1 0 console-consumer-c5ff906d-9abc-40d2-83d1-134ead27a552 /127.0.0.1 console-consumer