繁体   English   中英

如何动态获取主题名称然后从中读取

[英]how to get topic name dynamically and then read from it

我有一个需要动态读取主题名称的场景。 有什么办法可以获取域中所有主题的名称,然后一一读取它们的消息吗?

     @KafkaListener(groupId="something")
     someMethod(List<topics> topics){
        switchTo(topics.get(0)){
          read messages}
        switchTo(topics.get(1)){
          read messages}
       ...
       ...
       ...
     }

您所需要的listTopics来自AdminClient listTopics

Properties properties = new Properties();
properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka-host:9092");

AdminClient adminClient = AdminClient.create(properties);

System.out.println(adminClient.listTopics(new ListTopicsOptions()).names().get());

如果您可以使用 Kafka AdminClient,它会为listTopics方法提供一些重载,以获取有关集群中所有主题的信息。 请参阅此处以供参考: https : //kafka.apache.org/24/javadoc/index.html?org/apache/kafka/clients/admin/AdminClient.html

卡夫卡消费者可以订阅正则表达式模式...

如果您需要基于主题名称的特定逻辑,我建议您为这些用例创建单独的方法或应用程序,并在@KafkaListener注释中指定名称

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM