问题
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