Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list Kafka consumer group using python

I want to get Kafka consumer group list with python but I couldn't.

I use to zookeeper python client( kazoo) but consumer group list empty because this method for old consumer and we are not using old consumer.

How can I get consumer group list with python code?

./kafka-consumer-groups.sh  -bootstrap-server localhost:9092 -list
like image 537
BigData Avatar asked Jul 29 '26 00:07

BigData


1 Answers

You can easily list consumer groups with kafka-python.

Just send a ListGroupsRequest to any of the brokers in your cluster.

For example:

from kafka import BrokerConnection
from kafka.protocol.admin import *
import socket

bc = BrokerConnection('localhost', 9092, socket.AF_INET)
bc.connect_blocking()

list_groups_request = ListGroupsRequest_v1()

future = bc.send(list_groups_request)
while not future.is_done:
    for resp, f in bc.recv():
        f.success(resp)

for group in future.value.groups:
    print(group)
like image 135
Mickael Maison Avatar answered Jul 31 '26 14:07

Mickael Maison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!