MergeTree 转换为 ReplicatedMergeTree 官方路径 数据副本 | ClickHouse Docs
我们使用 MergeTree 来表示 MergeTree系列 中的所有表引擎,ReplicatedMergeTree 同理。
如果你有一个手动同步的 MergeTree 表,您可以将其转换为可复制表。如果你已经在 MergeTree 表中收集了大量数据,并且现在要启用复制,则可以执行这些操作。
如果各个副本上的数据不一致,则首先对其进行同步,或者除保留的一个副本外,删除其他所有副本上的数据。
重命名现有的 MergeTree 表,然后使用旧名称创建 ReplicatedMergeTree 表。 将数据从旧表移动到新表(/var/lib/clickhouse/data/db_name/table_name/)目录内的 ‘detached’ 目录中。 然后在其中一个副本上运行ALTER TABLE ATTACH PARTITION,将这些数据片段添加到工作集中。
根据官方文档 :
1、重命名 MergeTree 表
2、使用旧名称创建 ReplicatedMergeTree 表
3、将数据从旧表移动到新表(/var/lib/clickhouse/data/db_name/table_name/)目录内的 ‘detached’ 目录中
4、运行ALTER TABLE ATTACH PARTITION,将这些数据片段添加到工作集中
假设test库都为MergeTree 表 以下是将整个库更改为 ReplicatedMergeTree 表shell
#!/bin/bash
# Configuration variables
CLICKHOUSE_USER="default"
CLICKHOUSE_PASSWORD="123456"
CLICKHOUSE_HOST="localhost"
CLICKHOUSE_PORT="9000"
CLICKHOUSE_DATABASE="test"
CLUSTER="ck_1shard_2replica_cluster"
DATAPATH="/var/lib/clickhouse/data/test" #需要更改的单节点数据库路径
# 获取指定数据库中的表列表
tables=$(clickhouse-client --user=$CLICKHOUSE_USER --password=$CLICKHOUSE_PASSWORD --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT -q "SHOW TABLES FROM $CLICKHOUSE_DATABASE FORMAT TabSeparated")
# 遍历每个表
for table in $tables; do
# 移除制表符(如果有的话)
table=$(echo "$table" | tr -d '\t')
new_table="${table}_old"
# 重命名表
clickhouse-client --user=$CLICKHOUSE_USER --password=$CLICKHOUSE_PASSWORD --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT -q "RENAME TABLE $CLICKHOUSE_DATABASE.$table TO $CLICKHOUSE_DATABASE.$new_table"
if [ $? -ne 0 ]; then
echo "Error renaming table $table to $new_table"
exit 1
fi
# 获取旧表的创建语句
create_table_query=$(clickhouse-client --user=$CLICKHOUSE_USER --password=$CLICKHOUSE_PASSWORD --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT -q "SHOW CREATE TABLE $CLICKHOUSE_DATABASE.$new_table FORMAT TabSeparated")
# 将创建表的 MergeTree引擎改为 ReplicatedMergeTree 此处省略('/clickhouse/tables/{shard}/{database}/{table}', '{replica}') 这部分在配置文件中已定义<default_replica_path>
create_table_statement=$(echo "$create_table_query" | sed 's/MergeTree/ReplicatedMergeTree/g' | sed 's#\\n# #g')
# 替换表名 增加集群创建表选项
full_create_table_query=$(echo "$create_table_statement" | sed "s/$new_table/$table ON CLUSTER $CLUSTER/g")
# 创建新表
clickhouse-client --user=$CLICKHOUSE_USER --password=$CLICKHOUSE_PASSWORD --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT -q "$full_create_table_query"
if [ $? -ne 0 ]; then
echo "Error creating table $table"
exit 1
fi
done
# 进入数据文件目录将旧表数据文件移到新表detached 目录下
cd $DATAPATH
for i in `ls | grep old | sed 's/_old$//'`;do cp -a "$i"_old/all* "$i"/detached/ ; done
#其中一个副本上运行ALTER TABLE ATTACH PARTITION,将这些数据片段添加到工作集中。
clickhouse-client --password $CLICKHOUSE_PASSWORD --format=TSVRaw -q"select 'ALTER TABLE ' || database || '.' || table || ' ATTACH PARTITION ID \'' || partition_id || '\';\n' from system.detached_parts group by database, table, partition_id order by database, table, partition_id;" | clickhouse-client --password $CLICKHOUSE_PASSWORD -mn
echo "All tables have been renamed and (structure of) replicated successfully."