kafka 获取主题的分区数 [英] kafka get partition count for a topic

查看:138
本文介绍了kafka 获取主题的分区数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从代码中获取任何 kafka 主题的分区数.我研究了很多链接,但似乎都没有.

How can I get number of partitions for any kafka topic from the code. I have researched many links but none seem to work.

提几个:

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

看起来类似的讨论.

SO 上也有类似的链接,但没有针对此问题的有效解决方案.

Also there are similar links on SO which do not have a working solution to this.

推荐答案

在 0.82 Producer API 和 0.9 Consumer api 中,您可以使用类似的东西

In the 0.82 Producer API and 0.9 Consumer api you can use something like

Properties configProperties = new Properties();
configProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");
configProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.ByteArraySerializer");
configProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");

org.apache.kafka.clients.producer.Producer producer = new KafkaProducer(configProperties);
producer.partitionsFor("test")

这篇关于kafka 获取主题的分区数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆