生产者或管理员创建Kafka主题后,如何更改该主题的副本数?
Answers:
编辑:我被证明是错误的-请检查 ŁukaszDumiszewski的出色回答。
现在,我将保留原来的答案以供完善。
我认为你不能。通常情况是
./kafka-topics.sh --zookeeper本地主机:2181-更改--topic test2-复制因子3
但它说
选项“ [replication-factor]”不能与选项“ [alter]”一起使用
有趣的是,您可以动态更改分区数(在运行时完成时,这通常是破坏性很大的动作),但不能增加复制因子,而复制因子应该是透明的。但是请记住,它是0.10,而不是10.0 ...请参阅此处获取增强请求https://issues.apache.org/jira/browse/KAFKA-1543
要增加给定主题的副本数量,您必须:
例如,您可以创建cremental-replication-factor.json并将其放入其中:
{"version":1,
"partitions":[
{"topic":"signals","partition":0,"replicas":[0,1,2]},
{"topic":"signals","partition":1,"replicas":[0,1,2]},
{"topic":"signals","partition":2,"replicas":[0,1,2]}
]}
[或kafka - reassign - partitions.sh-取决于kafka软件包]
例如:
$ kafka-reassign-partitions --zookeeper localhost:2181 --reassignment-json-file increase-replication-factor.json --execute
[或kafka-topics.sh-取决于kafka软件包]
$ kafka-topics --zookeeper localhost:2181 --topic signals --describe
Topic:signals PartitionCount:3 ReplicationFactor:3 Configs:retention.ms=1000000000
Topic: signals Partition: 0 Leader: 2 Replicas: 0,1,2 Isr: 2,0,1
Topic: signals Partition: 1 Leader: 2 Replicas: 0,1,2 Isr: 2,0,1
Topic: signals Partition: 2 Leader: 2 Replicas: 0,1,2 Isr: 2,0,1
另请参阅:官方文档中描述如何增加复制因子的部分。
{ "topics": [ { "topic": "YOUR_TOPIC_NAME_1" }, { "topic": "YOUR_TOPIC_NAME_2" } ], "version": 1 }
然后,命令如下kafka-reassign-partitions.sh --zookeeper #.#.#.#:2181,#.#.#.#:2181,#.#.#.#:2181 --broker-list #,#,# --topics-to-move-json-file reassignment.topics.json --generate
kafka-reassign-partitions
会导致任何停机时间?我有一些主题的复制因子为1(默认值,创建时忘了指定),我想知道我的生产者在重新分配分区时是否会出错。
如果要更改所有主题的复制因子,此脚本可能会为您提供帮助:
#!/bin/bash
topics=`kafka-topics --list --zookeeper zookeeper:2181`
while read -r line; do lines+=("$line"); done <<<"$topics"
echo '{"version":1,
"partitions":[' > tmp.json
for t in $topics; do
if [ "${t}" == "${lines[-1]}" ]; then
echo " {\"topic\":\"${t}\",\"partition\":0,\"replicas\":[0,1,2]}" >> tmp.json
else
echo " {\"topic\":\"${t}\",\"partition\":0,\"replicas\":[0,1,2]}," >> tmp.json
fi
done
echo ' ]
}' >> tmp.json
kafka-reassign-partitions --zookeeper zookeeper:2181 --reassignment-json-file tmp.json --execute
如果您有很多分区,使用kafka-reassign-partitions
ŁukaszDumiszewski的答案(和官方文档)所需的json文件生成可能会节省很多时间。这是一个从1到2个服务器复制64个分区主题的示例,而不必指定所有分区:
expand_topic=TestTopic
current_server=111
new_servers=111,222
echo '{"topics": [{"topic":"'${expand_topic}'"}], "version":1}' > /tmp/topics-to-expand.json
/bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --topics-to-move-json-file /tmp/topics-to-expand.json --broker-list "${current_server}" --generate | tail -1 | sed s/\\[${current_server}\\]/\[${new_servers}\]/g | tee /tmp/topic-expand-plan.json
/bin/kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file /tmp/topic-expand-plan.json --execute
/bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic ${expand_topic}
输出:
Topic:TestTopic PartitionCount:64 ReplicationFactor:2 Configs:retention.ms=6048000
Topic: TestTopic Partition: 0 Leader: 111 Replicas: 111,222 Isr: 111,222
Topic: TestTopic Partition: 1 Leader: 111 Replicas: 111,222 Isr: 111,222
....
@Дмитрий-Шепелев的脚本化答案未包含针对具有多个分区的主题的解决方案。此更新的版本可以:
#!/bin/bash
brokerids="1,2,3"
topics=`kafka-topics --list --zookeeper zookeeper:2181`
while read -r line; do lines+=("$line"); done <<<"$topics"
echo '{"version":1,
"partitions":['
for t in $topics; do
sep=","
pcount=$(kafka-topics --describe --zookeeper zookeeper:2181 --topic $t | awk '{print $2}' | uniq -c |awk 'NR==2{print $1}')
for i in $(seq 0 $[pcount - 1]); do
if [ "${t}" == "${lines[-1]}" ] && [ "$[pcount - 1]" == "$i" ]; then sep=""; fi
randombrokers=$(echo "$brokerids" | sed -r 's/,/ /g' | tr " " "\n" | shuf | tr "\n" "," | head -c -1)
echo " {\"topic\":\"${t}\",\"partition\":${i},\"replicas\":[${randombrokers}]}$sep"
done
done
echo ' ]
}'
注意:它还会随机分配代理,并为每个分区选择两个副本。因此,请确保在脚本中正确定义了代理ID。
执行如下:
$ ./reassign.sh > reassign.json
$ kafka-reassign-partitions --zookeeper zookeeper:2181 --reassignment-json-file reassign.json --execute
您也可以为此使用kafkactl:
# first run with --validate-only to see what kafkactl will do
kafkactl alter topic my-topic --replication-factor 2 --validate-only
# then do the replica reassignment
kafkactl alter topic my-topic --replication-factor 2
请注意,kafkactl为此使用的Kafka API仅适用于Kafka≥2.4.0。
免责声明:我是这个项目的贡献者
1.将所有主题复制到json文件
#!/bin/bash
topics=`kafka-topics.sh --zookeeper localhost:2181 --list`
while read -r line; do lines+=("$line"); done <<<"$topics"
echo '{"version":1,
"topics":['
for t in $topics; do
echo -e ' { "topic":' \"$t\" '},'
done
echo ' ]
}'
bash alltopics.sh > alltopics.json
2.运行kafka-reassign-partitions.sh生成重新平衡的文件
kafka-reassign-partitions.sh --zookeeper localhost:2181 --broker-list "0,1,2" --generate --topics-to-move-json-file alltopics.json > reassign.json
3.清理reassign.json文件,其中包含现有值和建议值
4.运行kafka-reassign-partitions.sh重新平衡主题
kafka-reassign-partitions.sh --zookeeper localhost:2181 --reassignment-json-file reassign.json --execute
要增加给定主题的副本数量,您必须:
1.使用以下命令为现有主题指定额外的分区(让我们说从2增加到3)
bin/kafktopics.sh --zookeeper localhost:2181 --alter --topic topic-to-increase --partitions 3
2.在自定义重新分配json文件中指定额外的副本
例如,您可以创建cremental-replication-factor.json并将其放入其中:
{"version":1,
"partitions":[
{"topic":"topic-to-increase","partition":0,"replicas":[0,1,2]},
{"topic":"topic-to-increase","partition":1,"replicas":[0,1,2]},
{"topic":"topic-to-increase","partition":2,"replicas":[0,1,2]}
]}
3.将文件与kafka-reassign-partitions工具的--execute选项一起使用
bin/kafka-reassign-partitions --zookeeper localhost:2181 --reassignment-json-file increase-replication-factor.json --execute
4.使用kafka-topics工具验证复制因子
bin/kafka-topics --zookeeper localhost:2181 --topic topic-to-increase --describe