Kafka-Kafka-Java extension query specifies all consumer-group s of topic

Keywords: Big Data kafka Apache Java Maven

Reference article:

https://www.bbsmax.com/A/n2d9bqDvzD/

 

The corresponding Kafka version: kafka_2.12-2.0.0.jar, namely scala 2.12, kafka 2.0.0 version.

 

Note the kafka version and recommend 1.0.0 + version.

We usually use the following way to query the group.id of all consumption:

 

kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list

[root@master ~]# kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
console-consumer-464
group

 

However, this method queries all group.id of all topics, but not group.id of all topics.

To accomplish this function, we can use kafka-java to implement it.

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>kafka-test</groupId>
    <artifactId>kafka-ext-api</artifactId>
    <version>1.0-SNAPSHOT</version>

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

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

 


Tools:

package com.test.kafka.util;

import kafka.admin.AdminClient;
import kafka.coordinator.group.GroupOverview;
import org.apache.kafka.common.TopicPartition;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by szh on 2018/10/23.
 *
 * @author szh
 */
public class KafkaUtil {

    public static Set<String> getAllGroupsForTopic(String brokerListUrl, String topic) {

        AdminClient client = AdminClient.createSimplePlaintext(brokerListUrl);

        try {
            List<GroupOverview> allGroups = scala.collection.JavaConversions.seqAsJavaList(client.listAllGroupsFlattened().toSeq());
            Set<String> groups = new HashSet<>();
            for (GroupOverview overview : allGroups) {
                String groupID = overview.groupId();
                
                // Version mismatch
                // org.apache.kafka.common.errors.UnsupportedVersionException: The broker only supports OffsetFetchRequest v1, but we need v2 or newer to request all topic partitions.
                // Map<TopicPartition, Object> offsets = scala.collection.JavaConversions.mapAsJavaMap(client.listGroupOffsets(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();
        }
    }

}

 

Ideas for realization:

Step1:

Query all groups.id

Step2:

Traversing through all groups.id, querying the consumption of each partition according to group.id, and topic name

Step3 :

Extract topic name from TopicPartition and add group id to set if topic name is the same as the specified topic name

Step4 : 

Return set < String >

 

 

Test class:

package com.test.kafka.util;

import org.junit.Test;

import java.util.Set;

/**
 * Created by szh on 2018/10/23.
 *
 * @author szh
 */
public class KafkaUtilTest {

    @Test
    public void testGetAllGroupsForTopic() {

//        //170 Cluster
//        // topic : topic_advert_impress
//        Set<String> consumerSet = KafkaUtil.getAllGroupsForTopic("10.170.0.6:9092,10.170.0.7:9092,10.170.0.8:9092", "topic_advert_impress");


        Set<String> consumerSet = KafkaUtil.getAllGroupsForTopic("192.168.75.128:9092", "test_find");


        for (String tmp : consumerSet) {
            System.out.println(tmp);
        }


    }


}

 

 

Note the kafka version. If the version is too low, such as 0.10, the following error will occur

// Version mismatch
org.apache.kafka.common.errors.UnsupportedVersionException: The broker only supports OffsetFetchRequest v1, but we need v2 or newer to request all topic partitions.

 

 

 

Posted by speckledapple on Fri, 25 Jan 2019 15:21:13 -0800