简体   繁体   中英

How to read headers in kafka partitioner

I'd like to extend Kafka DefaultPartitioner to make a custom one. However, I find no way to access the message headers as the partitioning should be based on a value present there.

You cannot access headers in custom partitioner class. But maybe you can create ProducerRecord manually on basis of your header value.

ProducerRecord has many overloaded constructor definitions. Some of them have partition argument. There you can specify partition number which says in which partition your ProducerRecord will go.

eg ProducerRecord<String,String> rec = new ProducerRecord(topic, partitionNo, key, value);

Otherwise you will have to embed that specific header value inside key or value object and then access it inside partitioner class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM