如何从代码中获取任何kafka主题的分区数。我已经研究了许多链接,但似乎没有工作。
提到几个:
http://grokbase.com/t/kafka/users/148132gdzk/find-topic-partition-count-through-simpleclient-api
http://grokbase.com/t/kafka/users/151cv3htga/get-replication-and-partition-count-of-a-topic
http://qnalist.com/questions/5809219/get-replication-and-partition-count-of-a-topic
类似的讨论。
也有类似的链接,所以没有一个工作的解决方案。
15条答案
按热度按时间f0ofjuux1#
可以从zookeeper shell检索分区数
以下是示例:
u4dcyp6a2#
你可以探索
kafka.utils.ZkUtils
其中有许多方法旨在帮助提取有关集群的元数据。这里的答案很好,所以我只是为了多样性补充一下:bfrts1fy3#
@苏尼尔·帕蒂尔的回答没有回答其中的要点。你得知道名单的大小
producer.partitionsfor(“test”).size()
@vish4071与sunil毫无关联,您在问题中没有提到您正在使用consumerconnector。
x759pob24#
你可以像这样从zookeeper那里得到Kafka分区列表。这是真正的Kafka服务器端分区号。
您可以在消费代码处使用分区计数。
kupeojn65#
使用kafkaconsumer的分区列表
应该很有魅力。如果有更简单的方法从主题访问分区列表,请告诉我。
3j86kqsm6#
去你的房间
kafka/bin
目录。然后运行以下命令:
./kafka-topics.sh --describe --zookeeper localhost:2181 --topic topic_name
你应该看看你需要什么PartitionCount
.xytpbqjk7#
0tdrvxhp8#
因此,下面的方法适用于kafka0.10,它不使用任何生产者或消费者api。它使用kafka中scalaapi的一些类,比如zkconnection和zkutils。
xxb16uws9#
axkjgtzd10#
下面shell cmd可以打印分区数。在执行cmd之前,您应该在kafka bin目录中:
请注意,必须根据需要更改主题名称。您还可以使用if条件进一步验证此情况:
如果count为16,上面的cmd命令将打印有效分区。您可以根据需要更改计数。
falq053o11#
要获得分区列表,理想/实际的方法是使用adminclients api
它可以作为独立的java方法运行,没有生产者/使用者依赖关系。
brjng4g312#
我也遇到过同样的问题,我需要为一个主题获取分区。
在答案的帮助下,我从Zookeeper那里得到了信息。
这是我用scala编写的代码(但可以很容易地翻译成java)
使用这种方法,我可以访问有关Kafka主题的信息以及有关Kafka经纪人的其他信息。。。
从zookeeper中,您可以通过浏览来检查主题的分区数
/brokers/topics/MY_TOPIC_NAME/partitions
使用zookeeper-client.sh
要连接到zookeeper:这表明这个主题有3个分区
MY_TOPIC_NAME
68de4m5k13#
我是这样做的:
请注意,我只需要提取分区ID,但您还可以检索任何其他分区元数据,如
leader
,isr
,replicas
, ...以及
BrokerInfo
只是一个简单的pojohost
以及port
领域。00jrzges14#
在0.82生产者api和0.9消费者api中,您可以使用
mtb9vblg15#
在java代码中我们可以使用
AdminClient
得到一个主题的和部分。