Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decrease number partitions Kafka topic?

Tags:

I created a topic with 4 partitions on Kafka. (set default number.partition=4). Now I want to change number partition of this topic to 3. I've tried running

./bin/kafka-topics.sh --alter --zookeeper localhost:2181 --topic my-topic --partitions 3 

but there is no change. It still has 4 partitions. Anyone know about this?

like image 249
Ken Nguyen Avatar asked Aug 04 '17 03:08

Ken Nguyen


People also ask

How do I change the number of partitions in a Kafka topic?

If you want to change the number of partitions or replicas of your Kafka topic, you can use a streaming transformation to automatically stream all of the messages from the original topic into a new Kafka topic that has the desired number of partitions or replicas.

Can we decrease the partition once we added?

We can delete current topic and recreate new one with required partition to achieve this . Reason for this as if we decrease the partition it will be data loss .

Can you alter topic partitions in any way in Kafka?

Yes provided you are increasing partitions.


2 Answers

Apache Kafka doesn't support decreasing the partition number. You should see the topic as a whole and the partitions are a way for scaling out improving performance. So all data sent to topic flow to all partitions and removing one of them means data loss.

like image 159
ppatierno Avatar answered Sep 21 '22 21:09

ppatierno


You can't just delete a partition because that would lead to data loss and also the remaining data's keys would not be distributed correctly so new messages would not get directed to the same partitions as old existing messages with the same key.

For the above reasons Kafka does not support decreasing partition counts on an existing topic.

What you can do is to create a new topic with 3 partitions and then write an small program (or use an existing replication tool) to copy the data from the old 4 partition topic to the new 3 partition topic. That way you will be running everything through the same partitioner and all your keyed messages will end up in the right partition. Once you are satisfied the data is all copied then delete the original 4 partition topic.

If you must retain the same topic name as the original topic then just create a new topic with the original name, copy the data back from the repartitioned topic, and delete that temporary repartitioning topic.

like image 20
Hans Jespersen Avatar answered Sep 20 '22 21:09

Hans Jespersen