Apacheのカフカは、特定のトピックに関する消費者のリストを取得します

Ares91:

それはタイトルからゲストられるように、Javaで特定のトピックに関する消費者のリストを取得する方法はありますか?このようなトピックのリストを取得することがそれまでは今イム

    final ListTopicsResult listTopicsResult = adminClient.listTopics();
    KafkaFuture<Set<String>> kafkaFuture = listTopicsResult.names();
    Set<String> map = kafkaFuture.get();

しかし、私のhaventは、各トピックに関する消費者のリストを取得する方法を発見しました

カティアGorshkova:

私は最近、私のカフカクライアントツールの同じ問題を解決しました。それは簡単ではありませんが、私はコードから見つかった唯一の方法は、以下の通りであります:

Properties props = ...//here you put your properties
AdminClient kafkaClient = AdminClient.create(props);

//Here you get all the consumer groups
List<String> groupIds = kafkaClient.listConsumerGroups().all().get().
                       stream().map(s -> s.groupId()).collect(Collectors.toList()); 

//Here you get all the descriptions for the groups
Map<String, ConsumerGroupDescription> groups = kafkaClient.
                                               describeConsumerGroups(groupIds).all().get();
for (final String groupId : groupIds) {
    ConsumerGroupDescription descr = groups.get(groupId);
    //find if any description is connected to the topic with topicName
    Optional<TopicPartition> tp = descr.members().stream().
                                  map(s -> s.assignment().topicPartitions()).
                                  flatMap(coll -> coll.stream()).
                                  filter(s -> s.topic().equals(topicName)).findAny();
            if (tp.isPresent()) {
                //you found the consumer, so collect the group id somewhere
            }
} 

このAPIは、バージョン2.0から使用可能です。そこより良い方法は、おそらくですが、私は1つを見つけることができませんでした。あなたは私の上のコードも見つけることができますビットバケットを

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=224249&siteId=1