在制作人或管理员创建Kafka主题后,您将如何更改此主题的副本数量?
答案 0 :(得分:86)
要增加给定主题的副本数量,您必须:
例如,您可以创建 increase-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
另请参阅:the part of the official documentation that describes how to increase the replication factor。
答案 1 :(得分:10)
编辑:我被证明是错的 - 请查看ŁukaszDumiszewski的优秀答案。
我暂时离开原来的完整答案。
我认为你不能。通常它会像
./ kafka-topics.sh --zookeeper localhost:2181 --alter --topic test2 --rerelication-factor 3
但它说
选项“[replication-factor]”不能与选项“[alter]”
一起使用
有趣的是,您可以动态更改分区数(在运行时完成时通常会产生极大的破坏性操作),但无法增加复制因子,这应该是透明的。但请记住,它是0.10,而不是10.0 ...请在此处查看增强请求https://issues.apache.org/jira/browse/KAFKA-1543
答案 2 :(得分:1)
如果您有很多分区,使用kafka-reassign-partitions
来生成sukasz Dumiszewski的答案(和官方文档)所需的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
....
答案 3 :(得分:1)
@Дмитрий-Шепелев的脚本化答案未包含针对具有多个分区的主题的解决方案。此更新的版本可以:
#!/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
答案 4 :(得分:0)
ŁukaszDumiszewski的answer是正确的,但是手动生成该文件有些困难。 幸运的是,有一些简单方法可以实现@ŁukaszDumiszewski所说的话。
如果您使用的是kafka-manager
tool,则从2.0.0.2
版本开始,您可以在主题视图的Generate Partition Assignment
部分中更改复制因子。然后,您应该单击Reassign Partitions
以应用生成的分区分配(如果选择其他复制因子,则会收到警告,但之后您可以单击Force Reassign
)。
如果您安装了ruby,则可以使用此helper script
答案 5 :(得分:0)
如果您想更改所有主题的复制因子,此脚本可能会为您提供帮助:
#!/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
答案 6 :(得分: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
答案 7 :(得分:0)
您也可以使用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仅适用于≥2.4.0的Kafka。
免责声明:我是这个项目的贡献者
答案 8 :(得分:-2)
要增加给定主题的副本数,您必须:
1。使用以下命令为现有主题指定额外的分区(让我们说从2增加到3)
bin/kafktopics.sh --zookeeper localhost:2181 --alter --topic topic-to-increase --partitions 3
2。在自定义重新分配json文件中指定额外的副本
例如,您可以创建crease-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