car-receive/src/main/java/com/ruoyi/receive/kafka/config/MyPartitioner.java

35 lines
835 B
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.ruoyi.receive.kafka.config;
import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;
import org.apache.kafka.common.PartitionInfo;
import java.util.List;
import java.util.Map;
/**
* 自定义分区器
* @Author JCC
* @Date 2023/8/22 22:25
* @Description
*/
public class MyPartitioner implements Partitioner {
@Override
public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
List<PartitionInfo> partitionInfos = cluster.partitionsForTopic(topic);
int size = partitionInfos.size();
int parId = ((String) value).hashCode() % size;
return parId;
}
@Override
public void close() {
}
@Override
public void configure(Map<String, ?> map) {
}
}