简体   繁体   English

如何知道正在从列表中使用哪个kafka?

[英]How to know which kafka is being consumed from list?

I have a function for consuming Kafka.我有一个 function 用于消费 Kafka。 I consume multiple topics in the same function. But I don't know which topic is consumed同一个function消费了多个topic,但是不知道消费的是哪个topic

  @KafkaListener(topics = {"topic1","topic2"})
  public void kafkaConsume(String message) {
    //Print the kafka topic that is being consumed
    return;
  }

How do I print the kafka topic?如何打印kafka主题?

You can add @Headers parameter to get all the extra information you want within the message.您可以添加 @Headers 参数以获取消息中所需的所有额外信息。

For example:例如:

@KafkaListener(topics = "topicName")
public void listenWithHeaders(
   @Payload String message, 
   @Header(KafkaHeaders.TOPIC) String topic) {
       System.out.println("Received Message: " + message" + "from: " + topic);
}

You can get all this information in KafkaHeaders https://docs.spring.io/spring-kafka/api/org/springframework/kafka/support/KafkaHeaders.html您可以在 KafkaHeaders https://docs.spring.io/spring-kafka/api/org/springframework/kafka/support/KafkaHeaders.html中获取所有这些信息

Or alternatively, you can consume a ConsumerRecord<K, V> which has most of the information as well或者,您可以使用也包含大部分信息的 ConsumerRecord<K, V>

@KafkaListener(topics = "topicName")
public void listenConsumerRecord(ConsumerRecord<String, String> record) {
       System.out.println("Received Message: " + record.value() + "from: " + record.topic());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道kafka队列消耗了多少数据以及kafka队列主题中存在哪些数据? - How to know howmany data consumed from kafka queue and what are data existing inside kafka queue topic? 如何删除消费者已经消费的数据? 卡夫卡 - How to delete data which already been consumed by consumer? Kafka 卡夫卡消费者收到一条消息之前 - kafka consumer get a message which was consumed before 单个 kafka 消费者 - 从多个主题中读取 - 消费消息的顺序是什么 - Single kafka consumer - reading from mutliple topics - what is the order in which messages will be consumed 我们如何手动重置通过 Spring Boot Java 应用程序消耗的 kafka 主题的偏移量? - How can we manually reset the offset of a kafka topic which is consumed through a spring boot java application? 从消费消息中获取 Kafka 主题的名称 - Get the name of the Kafka topic from a consumed message 卡夫卡 stream 消费到 CSV - Kafka stream Consumed to CSV Kafka:从宿主机发布的事件不会被运行在 Docker 中的应用程序消费 - Kafka: events published from the host machine are not consumed by the application running in Docker 如何使用 kafka-python 生成的 kafka 消息? - How to consume message from kafka which was produced by kafka-python? 如何增加每批Spring Kafka Consumer消耗的消息数? - How to increase the number of messages consumed by Spring Kafka Consumer in each batch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM