简体   繁体   English

Kafka-python 获取主题的分区数

[英]Kafka-python get number of partitions for topic

I'm using: https://github.com/mumrah/kafka-python as a kafka api in Python.我正在使用: https://github.com/mumrah/kafka-python作为 Python 中的卡夫卡 api。 I want to get the number of partitions for a specified topic.我想获取指定主题的分区数。 How do I do that?我怎么做?

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 在Python 3.4.3 / kafka-python 0.9.3上测试

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): 我知道问题是陈旧的,但这是我提出的解决方案(使用Kazoo与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)

Python 3.8.10/kafka-python 2.0.2 Solution: Python 3.8.10/kafka-python 2.0.2 解决方案:

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 partitions_for_topic

For those of you using Confluent-Python or the enterprise API. 对于那些使用Confluent-Python或企业API的人。 This can be done this way: 这可以这样做:

def count_partitions(my_partitions) -> int:
    count = 0
    for part in my_partitions:
        count = count + 1
    return count

cluster_data: ClusterMetadata = producer.list_topics(topic=TOPIC)
    topic_data: TopicMetadata = cluster_data.topics[TOPIC]
    available_partitions: PartitionMetadata = topic_data.partitions
    print(count_partitions(available_partitions))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kafka-python 获取主题的复制因子 - Kafka-python Get replication factor for a topic Confluent kafka python API - 如何获取主题中的分区数 - Confluent kafka python API - how to get number of partitions in a topic 如何使用kafka-python计算主题中的记录(消息)数 - How to count number of records (message) in the topic using kafka-python 使用 kafka-python 检索主题中的消息 - retrieve messages in a topic using kafka-python 有没有办法使用 kafka-python 库创建日志压缩主题? - Is there a way to create log compacted topic using kafka-python library? 如何在使用 kafka-python 创建主题时添加主题级别日志保留期 - How to add topic level log retention period while creating a topic using kafka-python 通过 kafka-python 库检查 python 中是否存在 kafka 主题,不使用消费者和 shell 命令 - Check whether kafka topic exists or not in python via kafka-python libraries and without using consumer and shell commands kafka-python:通过运行并发进程/脚本同时产生和消费来自同一主题的消息 - kafka-python: produce and consume messages from same topic at the same time by running concurrent process/scripts Kafka-python连接丢失 - Kafka-python connection lost 将kafka(kafka-python)转储到txt文件中 - Dump the kafka (kafka-python) to a txt file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM