如何使用控制台生产者在Kafka 0.11中产生带有标头的消息? [英] How to produce messages with headers in Kafka 0.11 using console producer?

查看:58
本文介绍了如何使用控制台生产者在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.

您需要编写自己的小型应用程序.创建 ProducerRecord 时会传入 Headers .例如:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆