简体   繁体   English

是否可以将分区添加到 Kafka 0.8.2 中的现有主题

[英]Is it possible to add partitions to an existing topic in Kafka 0.8.2

I have a Kafka cluster running with 2 partitions.我有一个运行 2 个分区的Kafka集群。 I was looking for a way to increase the partition count to 3. However, I don't want to lose existing messages on the topic.我正在寻找一种将分区数增加到 3 的方法。但是,我不想丢失有关该主题的现有消息。 I tried stopping Kafka , modifying the server.properties file to increase the number of partitions to 3 and restart Kafka.我尝试停止Kafka ,修改server.properties文件以将分区数增加到 3 并重新启动 Kafka。 However, that does not seem to change anything.然而,这似乎并没有改变什么。 Using Kafka ConsumerOffsetChecker , I still see it is using only 2 partitions.使用 Kafka ConsumerOffsetChecker ,我仍然看到它只使用 2 个分区。 The Kafka version I am using is 0.8.2.2.我使用的Kafka版本是 0.8.2.2。 In version 0.8.1, there used to be a script called kafka-add-partitions.sh , which I guess might do the trick.在 0.8.1 版本中,曾经有一个名为kafka-add-partitions.sh的脚本,我想这可能会奏效。 However, I don't see any such script in 0.8.2.但是,我在 0.8.2 中没有看到任何这样的脚本。

  • Is there any way of accomplishing this?有没有办法做到这一点?

I did experiment with creating a whole new topic and for that one, it does seem to use 3 partitions as per the change in the server.properties file.我确实尝试过创建一个全新的主题,对于那个主题,它似乎确实根据server.properties文件中的更改使用了 3 个分区。 However, for existing topics, it doesn't seem to care.但是,对于现有的主题,它似乎并不关心。

Looks like you can use this script instead:看起来您可以改用脚本:

bin/kafka-topics.sh --zookeeper zk_host:port/chroot --alter --topic my_topic_name 
   --partitions 40 

In the code it looks like they do same thing:在代码中,它们看起来像做同样的事情:

 AdminUtils.createOrUpdateTopicPartitionAssignmentPathInZK(topic, partitionReplicaList, zkClient, true)

kafka-topics.sh executes this piece of code as well as AddPartitionsCommand used by kafka-add-partition script. kafka-topics.sh执行这段代码以及 kafka-add-partition 脚本使用的AddPartitionsCommand

However you have to be aware of re-partitioning when using key:但是,您必须在使用密钥时注意重新分区:

Be aware that one use case for partitions is to semantically partition data, and adding partitions doesn't change the partitioning of existing data so this may disturb consumers if they rely on that partition.请注意,分区的一个用例是对数据进行语义分区,并且添加分区不会更改现有数据的分区,因此如果消费者依赖该分区,这可能会打扰消费者。 That is if data is partitioned by hash(key) % number_of_partitions then this partitioning will potentially be shuffled by adding partitions but Kafka will not attempt to automatically redistribute data in any way.也就是说,如果数据按hash(key) % number_of_partitions进行分区,那么这个分区可能会通过添加分区而被打乱,但 Kafka 不会尝试以任何方式自动重新分配数据。

For anyone who wants solution for newer Kafka versions.Please follow this method.对于任何想要更新 Kafka 版本的解决方案的人。请遵循此方法。

Kafka's entire data retention and transfer policy depends on partitions so be careful about effects of increasing partitions. Kafka 的整个数据保留和传输策略取决于分区,因此请注意增加分区的影响。 (Kafka's newer versions display warning regarding this) Try to avoid configuration in which one broker has too many leader partitions. (Kafka 的较新版本对此显示警告)尽量避免一个代理有太多领导分区的配置。

There is simple 3 stage approach to this.对此有简单的 3 阶段方法。

Step 1: Increase the partitions in topics第一步:增加topics中的partitions

./bin/kafka-topics.sh --zookeeper localhost:9092 --alter --topic testKafka_5 --partitions 6

Step 2: Create a partitioning json file for given topic第 2 步:为给定主题创建分区 json 文件

{ "version":1, "partitions":[ {"topic":"testKafka_5","partition":0,"replicas":[0,1,2]}, {"topic":"testKafka_5","partition":1,"replicas":[2,1,0]}, {"topic":"testKafka_5","partition":2,"replicas":[1,2,0]}, {"topic":"testKafka_5","partition":3,"replicas":[0,1,2]}, {"topic":"testKafka_5","partition":4,"replicas":[2,1,0]}, {"topic":"testKafka_5","partition":5,"replicas":[1,2,0]} ]} { "version":1, "partitions":[ {"topic":"testKafka_5","partition":0,"replicas":[0,1,2]}, {"topic":"testKafka_5"," partition":1,"replicas":[2,1,0]}, {"topic":"testKafka_5","partition":2,"replicas":[1,2,0]}, {"topic" :"testKafka_5","partition":3,"replicas":[0,1,2]}, {"topic":"testKafka_5","partition":4,"replicas":[2,1,0] }, {"topic":"testKafka_5","partition":5,"replicas":[1,2,0]} ]}

Create file with newer partition and replicas.使用较新的分区和副本创建文件。 It's better to expand replicas to different brokers but they should be present within same cluster.最好将副本扩展到不同的代理,但它们应该存在于同一个集群中。 Take latency into consideration for distant replicas.考虑远程副本的延迟。 Transfer the given file to your Kafka.将给定的文件传输到您的 Kafka。

Step 3: Reassign partitions and verify第 3 步:重新分配分区并验证

./bin/kafka-reassign-partitions.sh --zookeeper localhost:9092 --reassignment-json-file bin/increase-replication-factor.json  --execute

./bin/kafka-reassign-partitions.sh --zookeeper localhost:9092 --reassignment-json-file bin/increase-replication-factor.json --verify

You can check the effects of your change using --describe command.您可以使用--describe命令检查更改的效果。

If you are using Kafka in Windows try this code for alter or add partition in topic如果您在 Windows 中使用 Kafka,请尝试使用此代码更改或在主题中添加分区

.\bin\windows\kafka-topics.bat --alter --zookeeper localhost:2181 --topic TopicName --partitions 20

or或者

.\bin\windows\kafka-topics.bat --alter --zookeeper localhost:2181 --topic TopicName --replica-assignment 0:1:2,0:1:2,0:1:2,2:1:0 --partitions 10

I think this question is a bit old, but still I will answer.我认为这个问题有点老了,但我仍然会回答。

If you have a Kafka topic but want to change the number of partitions or replicas, you can use a streaming transformation to automatically stream all the messages from the original topic into a new Kafka topic which has the desired number of partitions or replicas.如果您有一个 Kafka 主题但想要更改分区或副本的数量,您可以使用流式转换将所有消息从原始主题自动流式传输到具有所需分区或副本数量的新 Kafka 主题。

In my case the value zk_host:port/chroot for parameter --zookeeper threw the following exception:在我的情况下,参数--zookeeper的值zk_host:port/chroot引发了以下异常:

ERROR java.lang.IllegalArgumentException: Topic my_topic_name does not exist on ZK path zk_host:port/chroot.错误 java.lang.IllegalArgumentException:主题 my_topic_name 在 ZK 路径 zk_host:port/chroot 上不存在。

So, I tried the following and it worked:所以,我尝试了以下方法,它奏效了:

 bin/kafka-topics.sh --alter --zookeeper zk_host:port --topic my_topic_name --partitions 10

In

kafka_2.13-3.2.0

This worked for me:这对我有用:

/bin/kafka-topics.sh --bootstrap-server localhost:9092 --alter --topic apache_event_log_topic --partitions 4

Code to increase Kafka partition count, in SringBoot using AdminCLient使用 AdminCLient 在 SringBoot 中增加 Kafka 分区数的代码

public void updatePartitionCount(Topic topic,AdminClient adminClient){
        Map<String, NewPartitions> newPartitions = new HashMap<>();
        newPartitions.put(topic.getName(), NewPartitions.increaseTo(5));
        CreatePartitionsOptions options = new CreatePartitionsOptions();
        adminClient.createPartitions(newPartitions);
        System.out.println("in partition count update");

    }`````

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM