kafka生产者API中的标头信息

时间:2018-05-02 13:37:31

标签: java apache apache-kafka kafka-producer-api

我有一个json有效负载,我想在生产者Api

中作为标题发送
Content-Type: application/xml

以上有效负载应作为标头发送

{
    "type": "record_created",
    "version": 1,
    "orgId": "",
    "userId": "",
    "userName": "",
    "correlationId": "",
    "jobId": ""
}

我们怎么做?

1 个答案:

答案 0 :(得分:1)

有两种方式

1) getHeaders并添加标题

ProducerRecord<Integer, String> record = new ProducerRecord<Integer, String>("topic", 1, "message");
record.headers().add(new RecordHeader("type", "record_created".getBytes()));
record.headers().add(new RecordHeader("type", "record_created".getBytes()));    
producer.send(record);

2) 使用ProducerRecord(String topic,Integer partition,K key,V value,Iterable headers)

但这样你必须决定分区 - 否