kafka producer partitions分区器(七)
消息在经过拦截器、序列化后,就需要确定它发往哪个分区,如果在ProducerRecord中指定了partition字段,那么就不再需要partitioner分区器进行分区了,如果没有指定,那么会根据key来将数据进行分区,如果partitioner和key都没有指定,那么就会采用默认的方式进行数据分区。 有没有指定partition可以从源码中看出: public ProducerRecord(String topic, Integer partition, K key, V value) {} 如果指定的partition,那就指定了数据发往哪个分区上,如果没有就会根据key来进行数据分区,如果2个都没有,那么会采用默认的分区策略来进行数据分区 Demo: public class CustomPartitioner { private static final Logger LOG = LoggerFactory.getLogger(CustomPartitioner.class); public static void main(String[] args) { //1.加载配置信息 Properties prop = loadProperties(); //2.创建生产者 KafkaProducer<Integer,String> producer = new