Kafka生产者之自定义分区器

1. 没有设置key
我们的消息会被轮训的发送到不同的分区
2. 设置了key
kafka自带的分区器,会根据key计算出来一个hash值,这个hash值会对应某一个分区
【Kafka生产者之自定义分区器】如果key相同的,那么hash值必然相同,key相同的值,必然是会被发送到同一个分区
3. 自定义分区器

package com.bigdata.test; 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; import java.util.Random; /** * Copyright (c) 2019 bigdata ALL Rights Reserved * Project: learning * Package: com.bigdata.test * Version: 1.0 * * @author qingzhi.wu 2020/8/1 17:01 */ public class MyPartitionor implements Partitioner { private Random random; public int partition(String topic, Object keyObj, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) { String key = (String) keyObj; //获取这个分区的所有信息 List partitionInfos = cluster.availablePartitionsForTopic(topic); //获取到分区个数 int count = partitionInfos.size(); //热点分区 int hotDataPartition = count - 1; return !key.contains("hot_data") ? random.nextInt(count - 1):hotDataPartition; }public void close() {}public void configure(Map, ?> configs) { random = new Random(); } }

    推荐阅读