gpt4 book ai didi

java - 增加 Java 中主题的分区数量

转载 作者:行者123 更新时间:2023-12-02 09:23:29 25 4
gpt4 key购买 nike

我使用的名称:kafka_2.12版本:2.3.0。根据流量/负载,我想更改主题的最大分区数。 Kafka启动后是否可以进行这种更改,并且可以通过代码完成吗?

最佳答案

是的,您可以通过代码增加分区。使用AdminClient.createPartitions方法。

AdminClients.createPartitions method API document

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. 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.

使用方法:

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();
}

关于java - 增加 Java 中主题的分区数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58505154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com