topic分区迁移
kafka-reassign-partitions.sh工具来重新分布分区。该工具有三种使用模式:
generate模式,给定需要重新分配的Topic,自动生成reassign plan(并不执行)
execute模式,根据指定的reassign plan重新分配Partition
verify模式,验证重新分配Partition是否成功
1 、生成推荐配置脚本
在进行分区副本重分配之前,最好是用下面方式获取一个合理的分配文件;
编写move-json-file.json文件; 这个文件就是告知想对哪些topic进行重新分配的计算。
$ cat move-json-file.json
{
"topics": [
{"topic": "topic_zyw_output"}
],
"version": 1
}
--topics-to-move-json-file move-json-file.json :指定编写的json文件
--broker-list "0,2" : 指定想要分配的broker节点 可以是多个"0,1,2"
--关键参数--generate
./kafka-reassign-partitions.sh --zookeeper 40.18.60.51:2181 --topics-to-move-json-file move-json-file.json --broker-list "0,2" --generate
结果:
Current partition replica assignment #当前分区副本分配
{"version":1,"partitions":[{"topic":"topic_zyw_output","partition":0,"replicas":[1],"log_dirs":["any"]}]}
Proposed partition reassignment configuration #建议的分区重新分配配置
{"version":1,"partitions":[{"topic":"topic_zyw_output","partition":0,"replicas":[0,2],"log_dirs":["any"]}]}
2、把建议的的配置转成json----https://www.sojson.com/
{
"version": 1,
"partitions": [{
"topic": "topic_zyw_output",
"partition": 0,
"replicas": [0,2],
"log_dirs":["any"] #迁移可以使用--execute、扩容副本不需要加、加了可能报错
}
}
3、转换后的json写入到另外一个文件ex.json
–execute 关键参数
[zyb@TESTU6AP05 bin]$ vim ex.json
{
"version": 1,
"partitions": [{
"topic": "topic_zyw_output",
"partition": 0,
"replicas": [0,2] #没有逗号 选择分区和副本在哪个节点
}]
}
[zyb@TESTU6AP05 bin]$ ./kafka-reassign-partitions.sh --zookeeper 40.18.60.51:2181 --reassignment-json-file ex.json --execute
Current partition replica assignment
"partition":0 : topic第几个分区
"replicas":[0,2]: broker节点 第几个节点
{"version":1,"partitions":[{"topic":"topic_zyw_output","partition":0,"replicas":[0,2],"log_dirs":["any"]}]}
Save this to use as the --reassignment-json-file option during rollback
Successfully started reassignment of partitions.
查看分配情况
[zyb@TESTU6AP05 bin]$ ./kafka-reassign-partitions.sh --zookeeper 40.18.60.51:2181 --reassignment-json-file ex.json --verify
Status of partition reassignment:
Reassignment of partition topic_zyw_output-0 completed successful
4、查看验证
目标是分配到 broker 为0的节点、
broker 0节点: 是已经分配过来了
[zyb@TESTU6AP05 bin]$ ls /home/logs/data/kafka/ | grep zyw
topic_zyw_output-0
broker 1节点: 原本在1节点 。迁移之后 分区目录名又了-delete的标识会被删除
[zyb@TESTU6AP04 ~]$ ls /home/logs/data/kafka/ | grep zyw
topic_zyw_output-0.e03ad5f9452b4dfcbdfa95eedd3de41c-delete
broker 2 节点: 是已经分配过来了
过一会就会被删掉了
[zyb@TESTU6AP02 ~]$ ls /home/logs/data/kafka/ | grep zyw
topic_zyw_output-0
多分区迁移和单分区一样只是验证和–generate 命令时候输出的、json有一些区别,比单分区会多一些内容 。 步骤一样
1、分区扩容
./kafka-topics.sh --zookeeper 40.18.60.51:2181 -alter --partitions 4 --topic topic_zyw_output
2、查看topic信息
[zyb@TESTU6AP05 bin]$ ./kafka-topics.sh --zookeeper 40.18.60.51:2181 --describe --topic topic_zyw_output
Topic: topic_zyw_output PartitionCount: 2 ReplicationFactor: 2 Configs: max.message.bytes=1048576
Topic: topic_zyw_output Partition: 0 Leader: 0 Replicas: 0,2 Isr: 2,0
Topic: topic_zyw_output Partition: 1 Leader: 1 Replicas: 1,2 Isr: 1,2
3、副本扩容
[zyb@TESTU6AP05 bin]$ ./kafka-reassign-partitions.sh --zookeeper 40.18.60.51:2181 --reassignment-json-file ex.json --execute
{
"version": 1,
"partitions":
[
{
"topic": "topic_zyw_output",
"partition": 0, #分区名
"replicas": [1,2] #没有逗号 选择分区和副本在哪个节点
},
{
"topic": "topic_zyw_output",
"partition": 1, #分区名
"replicas": [1,2] #没有逗号 多个分区
}
]
}