如何使用控制台生产者在 Kafka 0.11 中生成带有标题的消息? [英] How to produce messages with headers in Kafka 0.11 using console producer?
本文介绍了如何使用控制台生产者在 Kafka 0.11 中生成带有标题的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用控制台生产者在 Kafka 0.11 中生成带有标题的消息?
How to produce messages with headers in Kafka 0.11 using console producer?
我在 Kafka 文档中没有找到任何关于此的描述.
I didn't find any description in Kafka document about this.
推荐答案
使用 kafka-console-producer.sh
工具 (ConsoleProducer.scala
) 无法生成消息带有标题.
Using the kafka-console-producer.sh
tool (ConsoleProducer.scala
) you cannot produce messages with headers.
您需要编写自己的小应用程序.Headers
在创建 ProducerRecord
时传入.例如:
You need to write your own small application. Headers
are passed in when creating a ProducerRecord
. For example:
public static void main(String[] args) throws Exception {
Properties producerConfig = new Properties();
producerConfig.load(new FileInputStream("producer.properties"));
KafkaProducer<String, String> producer = new KafkaProducer<>(producerConfig);
List<Header> headers = Arrays.asList(new RecordHeader("header_key", "header_value".getBytes()));
ProducerRecord<String, String> record = new ProducerRecord<>("topic", 0, "key", "value", headers);
Future<RecordMetadata> future = producer.send(record);
future.get();
producer.close();
}
这篇关于如何使用控制台生产者在 Kafka 0.11 中生成带有标题的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文