简体   繁体   English

在 Java 中增加一个主题的分区数

[英]Increase number of partitions for a topic in Java

I am using name: kafka_2.12 version: 2.3.0 .我正在使用名称: kafka_2.12版本: 2.3.0 Based on the traffic/load I want to change the maximum partition number for a topic.根据流量/负载,我想更改主题的最大分区号。 Is it possible to make this kind of change once Kafka is up and can it be done by code?卡夫卡启动后是否可以进行这种更改并且可以通过代码完成?

Yes you could increase partition by code.是的,您可以通过代码增加分区。 Use AdminClient.createPartitions method.使用AdminClient.createPartitions 方法。

AdminClients.createPartitions method API document AdminClients.createPartitions 方法 API 文档

public abstract CreatePartitionsResult createPartitions(java.util.Map<java.lang.String,NewPartitions> newPartitions,CreatePartitionsOptions options)

Increase the number of partitions of the topics given as the keys of newPartitions according to the corresponding values.根据对应的值增加作为 newPartitions 的键给定的主题的分区数。 If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected.如果为具有键的主题增加分区,则分区逻辑或消息的顺序将受到影响。

This operation is not transactional so it may succeed for some topics while fail for others.此操作不是事务性的,因此它可能对某些主题成功,而对另一些主题则失败。

It may take several seconds after this method returns success for all the brokers to become aware that the partitions have been created.在此方法返回成功后,所有代理可能需要几秒钟才能意识到已创建分区。 During this time, describeTopics(Collection) may not return information about the new partitions.在此期间,describeTopics(Collection) 可能不会返回有关新分区的信息。

How to use:如何使用:

public static void createPartitions(String topicName, int numPartitions) {
    Properties props = new Properties();
    props.put("bootstrap.servers","localhost:9092");
    AdminClient adminClient = AdminClient.create(props);

    Map<String, NewPartitions> newPartitionSet = new HashMap<>();
    newPartitionSet.put(topicName, NewPartitions.increaseTo(numPartitions));
    adminClient.createPartitions(newPartitionSet);
    adminClient.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kafka:使用Java更改特定主题的分区数 - Kafka : Alter number of partitions for a specific topic using java Kafka 1.0:使用Java更改特定主题的分区数 - Kafka 1.0 : Alter number of partitions for a specific topic using java 如何使用 Java 查找主题中存在的消息数、主题的分区数? - How to find number of messages present in topic , number of partitions present for a topic using Java? 如何增加Kafka主题的分区数 - How to increase the number of partition for a Kafka Topic Kafka无法创建具有大量分区(64k)的主题 - Kafka fails to create Topic with large number of partitions (64k) 用Java中的字符增加数字 - Increase number with character in Java 在使用 kafka 和 Spark 流创建直接流之前获取主题的分区数? - Get number of partitions for a topic before creating a direct stream using kafka and spark streaming? spring cloud stream kafka binder 以编程方式将 kafka 主题(多个分区)偏移量重置为任意数字 - spring cloud stream kafka binder resetting a kafka topic(multiple partitions) offset to an arbitrary number programatically Java Hex数量增加一 - Java Hex number increase by one Kafka:从消费者方面动态确定主题中分区数量的最佳方法是什么? - Kafka: what's the best way to dynamically determine the number of partitions in a topic from the consumer side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM