我們是否支持kafka 0.8.0版本中的生產者的partitionsFor
方法?我想用這種方法來獲得給定kafka主題的分區數量。使用分區獲取kafka 0.8.0中生產者的分區數量對於方法
如果此方法在kafka 0.8.0中不可用,那麼在此特定版本的kafka中獲取生產者中分區數的最簡單方法是什麼?
我們是否支持kafka 0.8.0版本中的生產者的partitionsFor
方法?我想用這種方法來獲得給定kafka主題的分區數量。使用分區獲取kafka 0.8.0中生產者的分區數量對於方法
如果此方法在kafka 0.8.0中不可用,那麼在此特定版本的kafka中獲取生產者中分區數的最簡單方法是什麼?
您可以使用listTopics()方法也
ArrayList<Topics> topicList = new ArrayList<Topics>();
Properties props = new Properties();
Map<String, List<PartitionInfo>> topics;
Topics topic;
InputStream input =
getClass().getClassLoader().getResourceAsStream("kafkaCluster.properties");
try {
props.load(input);
props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,
props.getProperty("BOOTSTRAP_SERVERS_CONFIG"));
props.put("key.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<String,
String>(props);
topics = consumer.listTopics();
// System.out.println(topics.get(topics));
for (Map.Entry<String, List<PartitionInfo>> entry :
topics.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " +
entry.getValue());
topic = new Topics();
topic.setTopic_name(entry.getKey());
topic.setPartitions(Integer.toString(entry.getValue().size()));
topicList.add(topic);
}
} catch (IOException e) {
e.printStackTrace();
}
你爲什麼不試試以下方法 https://stackoverflow.com/a/35458605/5922904。 另外ZkUtils有getPartitionsForTopics方法也可以使用。雖然我還沒有嘗試過,並測試它自己
在這裏,在數組元素的數量是分區的數量。 – Megha