简体   繁体   English

如何在 kafka 分区器中读取标头

[英]How to read headers in kafka partitioner

I'd like to extend Kafka DefaultPartitioner to make a custom one.我想扩展 Kafka DefaultPartitioner 来定制一个。 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.您无法访问自定义分区器 class 中的标头。 But maybe you can create ProducerRecord manually on basis of your header value.但也许您可以根据您的 header 值手动创建 ProducerRecord。

ProducerRecord has many overloaded constructor definitions. ProducerRecord 有许多重载的构造函数定义。 Some of them have partition argument.其中一些有分区参数。 There you can specify partition number which says in which partition your ProducerRecord will go.在那里你可以指定分区号,它表示你的 ProducerRecord 将 go 在哪个分区。

eg ProducerRecord<String,String> rec = new ProducerRecord(topic, partitionNo, key, value);例如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.否则,您必须在键或值 object 中嵌入特定的 header 值,然后在分区器 class 中访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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