簡體   English   中英

獲取與消費組kafka關聯的主題列表

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM