Home > database >  Get the name of the Kafka topic from a consumed message
Get the name of the Kafka topic from a consumed message

Time:07-21

  1. I am listening to multiple Kafka topics using regex pattern
  2. The name of the topics for eg worker_123_456_topic, worker_789_10112_topic etc
  3. I would like to grab 123_456 and 789_10112
  4. Hence is there anyway to know the name of the topic from the message consumed?

Any leads are much appreciated! Thanks

CodePudding user response:

From the Spring-Kafka API, in the Kafka message you should have an header called "RECEIVED_TOPIC" that contains the information you are looking for.

Check out this link: https://github.com/spring-projects/spring-kafka/issues/290

CodePudding user response:

Though other answers are correct, just explaining a little bit further :

Lets say, your key value pair is of type String, String then :

ConsumerRecords<String,String> recs = kafkaConsumer.poll();

Iterator<ConsumerRecord<String,String>> itr= recs.iterator();
while(itr.hasNext()){
   String topicName = itr.next().topic(); 
    //then do either split operation with topic name.
}

CodePudding user response:

From the Java API, you can access ConsumerRecord.topic() regardless of how you are subscribing to the data.

I would like to grab 123_456 and 789_10112

Given the string of the topic, use substring or split("_", 1) methods

  • Related