[英]Get list of topics associated with consumer group kafka
我有 20 个主题,在 20 个主题中,consumer1 使用了 5 个主题,consumer2 使用了 15 个主题。 有什么方法可以获取与每个消费者相关的主题列表。 如果我将消费者 ID 作为参数/请求传递,我想显示与该消费者关联的所有主题。
寻找 Java 或 restful 服务程序。
kafka-consumer-groups.sh --describe <<group.id>>
将显示组中所有消费者的列表,包括他们的 ID、组的主题部分和消费者的网络地址。 然后您可以通过client.id
进行grep
开箱即用, client.id
单独使用client.id
来发现消费者。
您可以使用管理客户端通过 listConsumerGroups 函数(或任何其他有关 kafka 的信息)获取有关消费者组的信息。 所有实用程序脚本都在后台使用 adminclient。
从AdminClient您可以使用listConsumerGroupOffsets
列出主题的消费者组偏移量
使用默认选项列出集群中可用的消费者组偏移量。 这是带有默认选项的 #listConsumerGroupOffsets(String, ListConsumerGroupOffsetsOptions) 的便捷方法。
ListConsumerGroupOffsetsResult list = client.listConsumerGroupOffsets("group");
然后你可以得到所有带有偏移量的主题和分区
KafkaFuture<Map<TopicPartition,OffsetAndMetadata>> topics = list.partitionsToOffsetAndMetadata()
final List<String> groupTopics =
adminClient.describeConsumerGroups(CollectionUtil.listOf(groupId))
.all().get().get(groupId).members() //get group members (consumers info)
.stream().flatMap(member ->
member.assignment() // get active assignment for the specific member
.topicPartitions()
.stream()
.map(TopicPartition::topic)
.collect(Collectors.toSet()) // get unique topics per consumer
.stream())
.collect(Collectors.toSet()); // get unique topics per group
log.info("Group {} has active topics: {}", groupId, groupTopics);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.