zoukankan      html  css  js  c++  java
  • 查询订阅某topic的所有consumer group(Java API)

    在网上碰到的问题,想了下使用现有的API还是可以实现的。

    首先,需要引入Kafka服务器端代码,比如加入Kafka 1.0.0依赖:

    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'

    然后编写获取订阅某topic的所有group的方法,代码如下:

    /**
         * get all subscribing consumer group names for a given topic
         * @param brokerListUrl localhost:9092 for instance
         * @param topic         topic name
         * @return
         */
        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();
                    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();
            }
        }  
  • 相关阅读:
    后期生成事件命令copy /y
    SevenZipShaper压缩类
    vs2017
    WCF路由服务
    微服务--
    各种流程图的绘画网路工具 processon
    ROC 准确率,召回率 F-measure理解(转载)
    Unix OpenCV安装
    转载:tar 解压缩命令~
    cppreference经验总结
  • 原文地址:https://www.cnblogs.com/huxi2b/p/7831787.html
Copyright © 2011-2022 走看看