全网最细~ Clickhouse 均衡节点数据脚本《大力仑出品必属精品》
脚本需要修改的内容:
new_hostname -> 新节点hostname
partition.txt -> 需要均衡的表分区
old_hostname -> 久节点hostname
gesaas.ych_v2 -> 需要均衡的库名.表名
/clickhouse/tables/01/gesaas/ych_v2 -> old_hostname节点存储在zk 上的元数据目录
执行脚本命令
sh clickhouse-remove.sh hsotname port default password ck_home/
#!/bin/bash
# 参数检查
if [ $# -ne 5 ]; then
echo "Usage: $0 <host_ip> <http_port> <user> <password> <ck_home/>"
exit 1
fi
host_ip=$1
http_port=$2
user=$3
password=$4
ck_home=$5
# 函数:迁移分区数据
function migratory() {
partition=$1
# Fetch
${ck_home}clickhouse-client --host new_hostname -q "alter table gesaas.ych_v2 fetch partition $partition from '/clickhouse/tables/01/gesaas/ych_v2'"
if [ $? -ne 0 ]; then
echo "Error: Failed to fetch partition $partition"
return 1
fi
# Attach
${ck_home}clickhouse-client --host new_hostname -q "alter table gesaas.ych_v2 attach partition '$partition'"
if [ $? -ne 0 ]; then
echo "Error: Failed to attach partition $partition"
return 1
fi
# Drop
${ck_home}clickhouse-client --host old_hostname -q "alter table gesaas.ych_v2 drop partition $partition"
if [ $? -ne 0 ]; then
echo "Error: Failed to drop partition $partition"
return 1
fi
echo "Successfully migrated partition $partition"
return 0
}
# 检查 partition.txt 文件是否存在
if [ ! -f partition.txt ]; then
echo "Error: partition.txt file not found!"
exit 1
fi
# 读取 partition.txt 文件中的每一行并调用 migratory 函数
while read -r line; do
if [ -n "$line" ]; then
migratory $line
else
echo "Warning: Empty line detected in partition.txt"
fi
done < partition.txt