Apache Kafka获取特定主题的使用者列表 [英] Apache Kafka get list of consumers on a specific topic
问题描述
因为它可以从标题中邀请,所以有没有一种方法可以获取有关Java中特定主题的使用者列表? 直到现在,我都无法获得像这样的主题列表
As it can be guest from the title, is there a way to get the consumer list on a specific topic in java? Untill now Im able to get the list of topics like this
final ListTopicsResult listTopicsResult = adminClient.listTopics();
KafkaFuture<Set<String>> kafkaFuture = listTopicsResult.names();
Set<String> map = kafkaFuture.get();
但是我还没有找到一种获取每个主题的消费者列表的方法
but I havent found a way to get the list of consumers on each topic
推荐答案
我最近正在为我的kafka客户端工具解决相同的问题.这并不容易,但是从代码中发现的唯一方法是:
I was recently solving the same problem for my kafka client tool. It is not easy, but the only way, which I found from the code is the following:
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中获得.也许有更好的方法,但是我找不到.您还可以在我的 bitbucket
This API is available from the version 2.0. There is probably a better way but I was not able to find one. You can also find the code on my bitbucket
这篇关于Apache Kafka获取特定主题的使用者列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!