问题
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