Querying all consumer group (Java APIs) that subscribe to a topic

Source: Internet
Author: User

Problems encountered on the Internet, think of the use of existing APIs can still be achieved.

First, you need to introduce Kafka server-side code, such as adding Kafka 1.0.0 dependencies:

Maven

<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.12</artifactId>
<version>1.0.0</version>
</dependency>

Gradle

Compile group: ' Org.apache.kafka ', Name: ' kafka_2.12 ', version: ' 1.0.0 '

Then write a method that gets all the group subscriptions to a topic, with the following code:

/** * Get all subscribing consumer group names for a given topic * @param brokerlisturl localhost:9092 for Instanc E * @param topic topic name * @return */public static set<string> Getallgroupsfortopic (Strin        G Brokerlisturl, String topic) {adminclient client = Adminclient.createsimpleplaintext (Brokerlisturl); try {list<groupoverview> allgroups = scala.collection.JavaConversions.seqAsJavaList (client.listallgroups            Flattened (). TOSEQ ());            set<string> groups = new hashset<> ();                for (Groupoverview overview:allgroups) {String GroupID = Overview.groupid (); Map<topicpartition, object> offsets = Scala.collection.JavaConversions.mapAsJavaMap (Client.listgroupoffsets (                GroupID));                set<topicpartition> partitions = Offsets.keyset ();             for (Topicpartition tp:partitions) {if (Tp.topic (). Equals (topic)) {           Groups.add (GroupID);        }}} return groups;        } finally {client.close ();   }    }

Querying all consumer group (Java APIs) that subscribe to a topic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.