Kafka multiple topic consume

女生的网名这么多〃 提交于 2021-02-04 19:31:26

问题


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

            }

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

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


回答1:


For regex use the following signature

KafkaConsumer.subscribe(Pattern pattern, ConsumerRebalanceListener listener)

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);


来源:https://stackoverflow.com/questions/44678708/kafka-multiple-topic-consume

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!