Search code examples
pythonapache-kafkaconfluent-kafka-python

Confluent kafka python API - how to get number of partitions in a topic


I would like to get the number of partitions within a topic but the API is difficult to understand at best.

I found the following, but, the topic information doesn't contain the numbers of partitions.

import confluent_kafka
from confluent_kafka.admin import AdminClient, ConfigResource

kafkaServers =  ["***","****"]

bootstrapServers = ",".join(kafkaServers)
adminClient = AdminClient({
    'bootstrap.servers': bootstrapServers
})

result = adminClient.describe_configs([ConfigResource(confluent_kafka.admin.RESOURCE_TOPIC, "model-detections-dev")])

config = list(result.values())[0].result()

enter image description here

how can I get the number of partitions?


Solution

  • Partitions are not a "topic config" to be gotten from the AdminClient.

    You can use a Consumer instance to get them

    consumer.list_topics() returns ClusterMetadata, which holds a map of topics to TopicMetadata, which has a partitions attribute. Take its length to find number of partitions.