我有一个需要动态读取主题名称的场景。有什么方法可以获取域中所有主题的名称,然后一一读取它们的消息?
@KafkaListener(groupId="something")
someMethod(List<topics> topics){
switchTo(topics.get(0)){
read messages}
switchTo(topics.get(1)){
read messages}
...
...
...
}
答案 0 :(得分:1)
您所需要的只是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());
答案 1 :(得分:0)
如果可以使用Kafka AdminClient,它将为listTopics
方法提供一些重载,以便获取有关集群中所有主题的信息。请参阅此处以供参考:https://kafka.apache.org/24/javadoc/index.html?org/apache/kafka/clients/admin/AdminClient.html
答案 2 :(得分:-1)
Kafka Consumer可以订阅正则表达式模式...
如果您需要基于主题名称的特定逻辑,建议您为这些用例创建单独的方法或应用程序,并在@KafkaListener
批注中指定名称