Arthur Fonzerelli
Arthur Fonzerelli

Reputation: 43

How to get Replication Factor of Kafka Topic from Java code

I have some topic name and i should to get info about it's replication factor from my Java application. I tried to do that with zookeeper library:

ZooKeeper zooKeeper = new ZooKeeper("localhost:22181", 10000, null);
String s = new String(zooKeeper.getData("/brokers/topics/" + KafkaTopicConfig.TOPIC_NAME, false, null));

But it has now info about replication factor, only info about current replicas count. I tried to do that with kafka-client api:

TopicDescription topicDescriptionKafkaFuture = describeTopicsResult.values().get(KafkaTopicConfig.TOPIC_NAME).get();

But there is also no info about topic's replication factor. Is there anyway to get that info from Java Code?

Upvotes: 3

Views: 3815

Answers (2)

ppatierno
ppatierno

Reputation: 10065

Starting from the describeTopics, throug the TopicDescription you end up with a list of TopicPartitionInfo which contains the replicas information.

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191738

Yes, it is possible. All the shell scripts are just Java (or Scala) code!

In the source code of kafka-topics --describe --topic, it looks at partitions(0).replicas

Upvotes: 2

Related Questions