Kafka-python get number of partitions for topic
Asked Answered
R

5

10

I'm using: https://github.com/mumrah/kafka-python as a kafka api in Python. I want to get the number of partitions for a specified topic. How do I do that?

Refractometer answered 13/4, 2015 at 18:14 Comment(0)
J
14

Might be a slightly simplistic solution, but:

from kafka import KafkaClient

client = KafkaClient('SERVER:PORT')
topic_partition_ids = client.get_partition_ids_for_topic(b'TOPIC')
len(topic_partition_ids)

tested on Python 3.4.3 / kafka-python 0.9.3

Jemena answered 18/5, 2015 at 14:7 Comment(3)
consider adding: a = len(topic_partition_ids) print(topic_partition_ids) print(a)Theron
this has been deprecated, try using "partitions_for_topic": kafka-python.readthedocs.io/en/master/apidoc/…Corned
As suggested by @Corned try to use : client.cluster.partitions_for_topic(topic)Coriss
O
3

For those of you using Confluent-Python or the enterprise API. This can be done this way:

cluster_data: ClusterMetadata = producer.list_topics(topic=TOPIC)
topic_data: TopicMetadata = cluster_data.topics[TOPIC]
available_partitions: PartitionMetadata = topic_data.partitions
print("Count of partitions:", len(available_partitions))
Orpah answered 28/6, 2019 at 16:32 Comment(0)
O
2

I found this question while trying to solve this exact same problem. I know the question is old, but here is the solution I came up with (using Kazoo to talk to zookeeper):

from kazoo.client import KazooClient

class KafkaInfo(object):
    def __init__(self, hosts):
        self.zk = KazooClient(hosts)
        self.zk.start()

    def topics(self):
        return self.zk.get_children('/brokers/topics')

    def partitions(self, topic):
        strs = self.zk.get_children('/brokers/topics/%s/partitions' % topic)
        return map(int, strs)

    def consumers(self):
        return self.zk.get_children('/consumers')

    def topics_for_consumer(self, consumer):
        return self.zk.get_children('/consumers/%s/offsets' % consumer)

    def offset(self, topic, consumer, partition):
        (n, _) = self.zk.get('/consumers/%s/offsets/%s/%d' % (consumer, topic, partition))
        return int(n)
Orozco answered 7/5, 2015 at 6:34 Comment(0)
H
2

Python 3.8.10/kafka-python 2.0.2 Solution:

from kafka import KafkaConsumer

def get_partitions_number(server, topic):
    consumer = KafkaConsumer(
        topic,
        bootstrap_servers=server
    )
    partitions = consumer.partitions_for_topic(topic)
    return len(partitions)

partitions_for_topic

Haughay answered 4/8, 2021 at 7:3 Comment(0)
P
0
from kafka import KafkaConsumer, TopicPartition

bootstrap_servers = "your_server"
topic = "your_topic"

consumer = KafkaConsumer(
    auto_offset_reset="earliest",
    bootstrap_servers=bootstrap_servers,
    api_version=(1, 10, 1),
)

consumer.assign([TopicPartition(topic, 0)])
number_of_partitions = len(consumer.partitions_for_topic(topic))

Penta answered 7/11, 2023 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.