Kafka 消费者 - 在反序列化之前读取消息头

时间:2021-05-23 05:28:43

标签: apache-kafka kafka-consumer-api

在反序列化之前有没有办法读取消息头?

我已经写了下面的代码,但我不得不在这里反序列化,有什么办法不反序列化?

    while (true) {

        ConsumerRecords<String, String> consumerRecords = consumer.poll(Duration.ofSeconds(1));
        for (ConsumerRecord<String, String> consumerRecord : consumerRecords) {
            for (Header header : consumerRecord.headers()) {
                if (header.key().equals("my header")) {
                    String data = "\n New record received .. \n" +
                            " Value: " + consumerRecord.value() +
                            " Topic: " + consumerRecord.topic() +
                            " Header: " + header.key() +
                            " Partition: " + consumerRecord.partition();

                    logger.info(data);
                }
            }
        }
    }

0 个答案:

没有答案