猿问

如果我不提任何,kafka如何决定分区

这就是我产生消息的方式:


String json = gson.toJson(msg);


ProducerRecord<String, String> record = new ProducerRecord<>(kafkaProducerConfig.getTopic(), json);

long startTime = System.currentTimeMillis();


try {

    RecordMetadata meta = producer.send(record).get(5, TimeUnit.SECONDS);

} catch (InterruptedException e) {

    e.printStackTrace();

} catch (ExecutionException e) {

    e.printStackTrace();

} catch (TimeoutException e) {

    e.printStackTrace();

}

我有15这个主题的分区,我在制作时没有提到分区键,分配的默认分区是什么?


婷婷同学_
浏览 198回答 2
2回答

蝴蝶不菲

如果您没有定义任何自定义分区,它将按照以下规则使用默认分区器如果记录中指定了分区,则使用该分区进行发布。如果未指定分区但存在键,则根据键的散列选择分区如果不存在分区或键,请以循环方式选择一个分区下面默认,Partition实现来更好的理解public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {&nbsp; &nbsp; &nbsp; &nbsp; List<PartitionInfo> partitions = cluster.partitionsForTopic(topic);&nbsp; &nbsp; &nbsp; &nbsp; int numPartitions = partitions.size();&nbsp; &nbsp; &nbsp; &nbsp; if (keyBytes == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nextValue = nextValue(topic);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<PartitionInfo> availablePartitions = cluster.availablePartitionsForTopic(topic);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (availablePartitions.size() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int part = Utils.toPositive(nextValue) % availablePartitions.size();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return availablePartitions.get(part).partition();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // no partitions are available, give a non-available partition&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Utils.toPositive(nextValue) % numPartitions;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // hash the keyBytes to choose a partition&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Utils.toPositive(Utils.murmur2(keyBytes)) % numPartitions;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答