0
votes

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.

1

1 Answers

0
votes

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.

e.g 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.