[英]Kafka Streams RoundRobinPartitioner
我寫了一個使用 kafka 2.4 kafka 客戶端版本和 kafka 2.2 服務器版本的 kafka 流代碼。 我的主題和內部主題有 50 個分區。
我的 kafka 流代碼有 selectKey() DSL 操作,我有 200 萬條記錄使用相同的 KEY。 在流配置中,我已經完成了
props.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, RoundRobinPartitioner.class);
這樣我就可以使用完全相同的密鑰使用不同的分區。 如果我沒有按預期使用循環,我的所有消息都會轉到同一個分區。
到目前為止一切都很好,但我意識到; 當我使用 RoundRobinPartitioner 類時,我的消息大約有 40 個分區。 10 個分區處於空閑狀態。 我想知道我錯過了什么? 它應該使用其中的 50 個大約 200 萬條記錄對嗎?
final KStream<String, IdListExportMessage> exportedDeviceIdsStream =
builder.stream("deviceIds");
// k: appId::deviceId, v: device
final KTable<String, Device> deviceTable = builder.table(
"device",
Consumed.with(Serdes.String(), deviceSerde)
);
// Some DSL operations
.join(
deviceTable,
(exportedDevice, device) -> {
exportedDevice.setDevice(device);
return exportedDevice;
},
Joined.with(Serdes.String(), exportedDeviceSerde, deviceSerde)
)
.selectKey((deviceId, exportedDevice) -> exportedDevice.getDevice().getId())
.to("bulk_consumer");
和
props.put(StreamsConfig.STATE_DIR_CONFIG, /tmp/kafka-streams);
props.put(StreamsConfig.REPLICATION_FACTOR_CONFIG, 3);
props.put(StreamsConfig.NUM_STANDBY_REPLICAS_CONFIG, 2);
props.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 100);
props.put(StreamsConfig.PROCESSING_GUARANTEE_CONFIG, EXACTLY_ONCE);
props.put("num.stream.threads", 10);
props.put("application.id", applicationId);
RoundRobinPartitioner.java
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() {
}
}
您不能使用ProducerConfig.PARTITIONER_CLASS_CONFIG
配置更改分區——這僅適用於普通生產者。
在 Kafka Streams 中,您需要實現接口StreamsPartitioner
並將您的實現傳遞給相應的操作符,例如to("topic", Produced.streamPartitioner(new MyPartitioner())
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.