Header information in kafka producer API

我是研究僧i 提交于 2020-05-17 05:54:23

问题


I have a json payload which i want to send as header in producer Api

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

Above payload should be sent as header

producer.send(new ProducerRecord<Integer, String>(topic, messageNo, records,header)

How can we do this ?


回答1:


There are two ways

1) getHeaders and add Header

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) use ProducerRecord(String topic, Integer partition, K key, V value, Iterable headers)

but this way you have to decide the partition-no



来源:https://stackoverflow.com/questions/50136040/header-information-in-kafka-producer-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!