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