You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@kafka.apache.org by Ankit Jain <an...@impetus.co.in> on 2015/01/12 09:31:28 UTC

Get replication and partition count of a topic

Hi All,


I want to get the replication and partition count of a topic. I tried the following piece of code:


        java.util.Set<String> topics = new HashSet<String>();
        topics.add("topicName");
        Set<TopicMetadata> topicMetadatas = AdminUtils.fetchTopicMetadataFromZk(JavaConversions.asScalaSet(topics), zkClient);
        Iterator<TopicMetadata> topicMetadataIterator = topicMetadatas.iterator();

        while (topicMetadataIterator.hasNext()) {
            topicMetadataIterator.next();
            topicMetadataIterator.next().
            Iterator<PartitionMetadata> partitionMetadataIterator = topicMetadataIterator.next().partitionsMetadata().iterator();

        }


But, the above code returning me the metadata of each partition and also replica details of each partition.


Is there any simple API available in kafka to get the partition and replica count for a topic.


Thanks,

Ankit


________________________________






NOTE: This message may contain information that is confidential, proprietary, privileged or otherwise protected by law. The message is intended solely for the named addressee. If received in error, please destroy and notify the sender. Any use of this email is prohibited when received in error. Impetus does not represent, warrant and/or guarantee, that the integrity of this communication has been maintained nor that the communication is free of errors, virus, interception or interference.

Re: Get replication and partition count of a topic

Posted by srikannan <sr...@yahoo.com>.
Ewen Cheslack-Postava <ew...@...> writes:

Im also searching the shortest way to find topic partition count, so that the 
initialization code in the thread pool can set up the right number of threads.

so far i found below is the shortest way. 


	public static void main(String[] args){
		
		int sessionTimeoutMs = 10000;
		int connectionTimeoutMs = 10000;
		ZkClient zkClient = new ZkClient("localhost:2181", 
sessionTimeoutMs, connectionTimeoutMs);

		TopicMetadata metaData =  
AdminUtils.fetchTopicMetadataFromZk("forthtopic",zkClient);
		System.out.println(metaData.partitionsMetadata().size());

	}

kindly reply if you find any other shortest way.





Re: Get replication and partition count of a topic

Posted by Ewen Cheslack-Postava <ew...@confluent.io>.
I think the closest thing to what you want is
ZkUtils.getPartitionsForTopics, which returns a list of partition IDs for
each topic you specify.

-Ewen

On Mon, Jan 12, 2015 at 12:55 AM, Manikumar Reddy <ku...@nmsworks.co.in>
wrote:

> Hi,
>
> kafka-topics.sh script can be used to retrieve topic information.
>
> Ex: sh kafka-topics.sh --zookeeper localhost:2181 --describe --topic TOPIC1
>
> You can look into TopicCommand.scala code
>
> https://git-wip-us.apache.org/repos/asf?p=kafka.git;a=blob;f=core/src/main/scala/kafka/admin/TopicCommand.scala;hb=HEAD
>
> On Mon, Jan 12, 2015 at 2:01 PM, Ankit Jain <an...@impetus.co.in>
> wrote:
>
> > Hi All,
> >
> >
> > I want to get the replication and partition count of a topic. I tried the
> > following piece of code:
> >
> >
> >         java.util.Set<String> topics = new HashSet<String>();
> >         topics.add("topicName");
> >         Set<TopicMetadata> topicMetadatas =
> > AdminUtils.fetchTopicMetadataFromZk(JavaConversions.asScalaSet(topics),
> > zkClient);
> >         Iterator<TopicMetadata> topicMetadataIterator =
> > topicMetadatas.iterator();
> >
> >         while (topicMetadataIterator.hasNext()) {
> >             topicMetadataIterator.next();
> >             topicMetadataIterator.next().
> >             Iterator<PartitionMetadata> partitionMetadataIterator =
> > topicMetadataIterator.next().partitionsMetadata().iterator();
> >
> >         }
> >
> >
> > But, the above code returning me the metadata of each partition and also
> > replica details of each partition.
> >
> >
> > Is there any simple API available in kafka to get the partition and
> > replica count for a topic.
> >
> >
> > Thanks,
> >
> > Ankit
> >
> >
> > ________________________________
> >
> >
> >
> >
> >
> >
> > NOTE: This message may contain information that is confidential,
> > proprietary, privileged or otherwise protected by law. The message is
> > intended solely for the named addressee. If received in error, please
> > destroy and notify the sender. Any use of this email is prohibited when
> > received in error. Impetus does not represent, warrant and/or guarantee,
> > that the integrity of this communication has been maintained nor that the
> > communication is free of errors, virus, interception or interference.
> >
>



-- 
Thanks,
Ewen

Re: Get replication and partition count of a topic

Posted by Manikumar Reddy <ku...@nmsworks.co.in>.
Hi,

kafka-topics.sh script can be used to retrieve topic information.

Ex: sh kafka-topics.sh --zookeeper localhost:2181 --describe --topic TOPIC1

You can look into TopicCommand.scala code
https://git-wip-us.apache.org/repos/asf?p=kafka.git;a=blob;f=core/src/main/scala/kafka/admin/TopicCommand.scala;hb=HEAD

On Mon, Jan 12, 2015 at 2:01 PM, Ankit Jain <an...@impetus.co.in>
wrote:

> Hi All,
>
>
> I want to get the replication and partition count of a topic. I tried the
> following piece of code:
>
>
>         java.util.Set<String> topics = new HashSet<String>();
>         topics.add("topicName");
>         Set<TopicMetadata> topicMetadatas =
> AdminUtils.fetchTopicMetadataFromZk(JavaConversions.asScalaSet(topics),
> zkClient);
>         Iterator<TopicMetadata> topicMetadataIterator =
> topicMetadatas.iterator();
>
>         while (topicMetadataIterator.hasNext()) {
>             topicMetadataIterator.next();
>             topicMetadataIterator.next().
>             Iterator<PartitionMetadata> partitionMetadataIterator =
> topicMetadataIterator.next().partitionsMetadata().iterator();
>
>         }
>
>
> But, the above code returning me the metadata of each partition and also
> replica details of each partition.
>
>
> Is there any simple API available in kafka to get the partition and
> replica count for a topic.
>
>
> Thanks,
>
> Ankit
>
>
> ________________________________
>
>
>
>
>
>
> NOTE: This message may contain information that is confidential,
> proprietary, privileged or otherwise protected by law. The message is
> intended solely for the named addressee. If received in error, please
> destroy and notify the sender. Any use of this email is prohibited when
> received in error. Impetus does not represent, warrant and/or guarantee,
> that the integrity of this communication has been maintained nor that the
> communication is free of errors, virus, interception or interference.
>