[英]Custom Partitioner for Plain Producer | Kafka Streams
我有一个 kafka 流应用程序
props.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, MyPartitioner.class);
或者
props.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, RoundRobinPartitioner.class);
这是一个用于将消息分发到不同分区的类,即使在 kafka 2.4 版本中使用相同的密钥
RoundRobinPartitioner 有这个实现:
public class RoundRobinPartitioner implements Partitioner {
private final ConcurrentMap<String, AtomicInteger> topicCounterMap = new ConcurrentHashMap();
public RoundRobinPartitioner() {
}
public void configure(Map<String, ?> configs) {
}
public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
List<PartitionInfo> partitions = cluster.partitionsForTopic(topic);
int numPartitions = partitions.size();
int nextValue = this.nextValue(topic);
List<PartitionInfo> availablePartitions = cluster.availablePartitionsForTopic(topic);
if (!availablePartitions.isEmpty()) {
int part = Utils.toPositive(nextValue) % availablePartitions.size();
return ((PartitionInfo)availablePartitions.get(part)).partition();
} else {
return Utils.toPositive(nextValue) % numPartitions;
}
}
private int nextValue(String topic) {
AtomicInteger counter = (AtomicInteger)this.topicCounterMap.computeIfAbsent(topic, (k) -> {
return new AtomicInteger(0);
});
return counter.getAndIncrement();
}
public void close() {
}
}
我的 Partitioner 由完全相同的代码但不同的分区方法实现组成,我的代码块是:
public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
List<PartitionInfo> partitions = cluster.partitionsForTopic(topic);
int numPartitions = partitions.size();
int nextValue = nextValue(topic);
return Utils.toPositive(nextValue) % numPartitions;
}
当我像这样配置消息时,在两种实现中都将消息分发到不同的分区,但从不使用某些分区。
我有 50 个分区,分区 14 和 34 从未收到过消息。 我的分区并非不可用。 它们是可用的。 当我将返回分区方法更改为 14 或 34 时,我的所有消息都会转到该分区。 可能是什么问题呢 ? 两种实现都没有按预期工作。
编辑 1:我已经尝试过使用普通生产者的 RoundRobinPartitioner。 结果是一样的。 生产者不能在分区之间平均生产消息,有些分区从未使用过。 可能是什么原因 ? 它不像缺少配置。
编辑 2:我已经调试了 RoundRobinPartitioner 并在返回处设置了一个断点。 当我只产生 1 条消息时,Producer 产生两次消息。 第一次尝试总是不成功,并且该消息不会进入任何分区。 当我在调试时点击continue 时ConcurrentMap 的索引增加1。生产者的第二次尝试成功。
partition() 方法在我找不到的地方被调用。
编辑 3:这可能与我没有覆盖的 onNewBatch 方法有关吗?
编辑 4:此实现适用于 kafka 客户端 2.2,但不适用于 2.4。 分区接口没有 onNewBatch 方法。 当 key 为 null 2.2 vs 2.4 时,DefaultPartitioner 实现发生了变化。 它可以与棒分区有关吗?
在 kafka 2.4 客户端版本中使用 UniformStickyPartitioner.class。 RoundRobinPartitioner.class 适用于 kafka 2.2 或更低版本。 在 2.4 版本中
props.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, UniformStickyPartitioner.class);
应该使用。 我认为这与新的 StickPartitioner 有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.