卡夫卡多主题消费 [英] Kafka multiple topic consume

查看:80
本文介绍了卡夫卡多主题消费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   consumer.subscribe(Pattern.compile(".*"),new ConsumerRebalanceListener() {
            @Override
            public void onPartitionsRevoked(Collection<TopicPartition> clctn) {

            }

            @Override
            public void onPartitionsAssigned(Collection<TopicPartition> clctn) {
            }            
        });

如何在apache/kafka中使用正则表达式使用所有主题? 我尝试了上面的代码,但是没有用.

How to consume all topics with regex in apache/kafka? I tried above code, but it didn't work.

推荐答案

对于正则表达式,请使用以下签名

For regex use the following signature

KafkaConsumer.subscribe(Pattern pattern, ConsumerRebalanceListener listener)

例如以下代码段使使用者可以收听带有前缀my_topics_

E.g. the following code snippet enables the consumer to listen to all topics with prefix my_topics_

ConsumerRebalanceListener listener = new ConsumerRebalanceListener() {

  @Override
  public void onPartitionsRevoked(Collection<TopicPartition> arg0) {
    // Don't need it now.
  }

  @Override
  public void onPartitionsAssigned(Collection<TopicPartition> arg0) {
    // Don't need it now.
  }
};

pattern = Pattern.compile("my_topics_.*");
kafkaConsumer.subscribe(pattern, listener);

这篇关于卡夫卡多主题消费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆