Kafka 增加TOPIC分区partition数量的方法

本文介绍在kafka中,增加分区partition的方法,不能减少分区数量,只能增加

原始TOPIC分区partition

根本没分区。

[root@rh7_210 kafka]#  bin/kafka-topics.sh --describe --topic gbase8a --bootstrap-server localhost:9092
Topic: gbase8a  TopicId: pGWySKY_SWehGsMXihJhkA PartitionCount: 1       ReplicationFactor: 1    Configs: segment.bytes=1073741824
        Topic: gbase8a  Partition: 0    Leader: 0       Replicas: 0     Isr: 0
[root@rh7_210 kafka]#

增加分区partition

通过--alter命令,和--partition 参数指定新分区数量。注意只能增加。

[root@rh7_210 kafka]#  bin/kafka-topics.sh --alter --partitions 3  --topic gbase8a --bootstrap-server localhost:9092

查看TOPIC增加分区partition的效果

变成了3个分区。


[root@rh7_210 kafka]#  bin/kafka-topics.sh --describe --topic gbase8a --bootstrap-server localhost:9092
Topic: gbase8a  TopicId: pGWySKY_SWehGsMXihJhkA PartitionCount: 3       ReplicationFactor: 1    Configs: segment.bytes=1073741824
        Topic: gbase8a  Partition: 0    Leader: 0       Replicas: 0     Isr: 0
        Topic: gbase8a  Partition: 1    Leader: 0       Replicas: 0     Isr: 0
        Topic: gbase8a  Partition: 2    Leader: 0       Replicas: 0     Isr: 0
[root@rh7_210 kafka]#

减少TOPIC的分区partition

报错,原有分区数量大于请求数量。Topic currently has 3 partitions, which is higher than the requested 2.


[root@rh7_210 kafka]#  bin/kafka-topics.sh --alter --partitions 2  --topic gbase8a --bootstrap-server localhost:9092
Error while executing topic command : Topic currently has 3 partitions, which is higher than the requested 2.
[2021-07-16 16:05:36,893] ERROR org.apache.kafka.common.errors.InvalidPartitionsException: Topic currently has 3 partitions, which is higher than the requested 2.
 (kafka.admin.TopicCommand$)
[root@rh7_210 kafka]