ClickHouse 集群迁移,你确认会吗

clickhouse 迁移的方案有很多,但是因为迁移稳单相对较少,很多人望而却步,这里为大家介绍3种方案

一 insert select from remote

详情

相关文档 https://clickhouse.com/docs/en/sql-reference/table-functions/remote/

INSERT INTO table SELECT * FROM remote('ip', 'db.table', 'user', 'password')   where ...                      

亲测,迁移亿级别一下的数据强烈推荐,不需要关心其他信息

但是 该方案和clickhouse 的写入性能相关,在去重的引擎的情况下性能相对较差,时间是ClickHouse 普通MergeTree 的好几倍

方案:

  1. 去重复 目标集群可以尝试先使用MergeTree来接受数据,然后通过一些其他方案来改为去重表引擎
  2. 大数据量但是有分区的表也可以尝试按照分区迁移拆分任务

优点:

简单方案

缺点:

数据量不宜过大

二 fetch part

详情

clickhouse 在目标集群配置 auxiliary_zookeepers 配置源集群zookeeper 的配置,进入可以访问源集群的元数据信息,通过fetch part 的方案,将源集群的数据fetch 到目标集群

腾讯云ClickHouse 21.8 也支持的通过ip:port fetch part ,这里也省去了auxiliary_zookeepers 的配置

个人之前有写入fetch part 文档 https://cloud.tencent.com/developer/article/1867322

官方文档 https://clickhouse.com/docs/en/sql-reference/statements/alter/partition/#fetch-partitionpart

<auxiliary_zookeepers>
    <source_zookeper>
        <node>
            <host>example_2_1</host>
            <port>2181</port>
        </node>
        <node>
            <host>example_2_2</host>
            <port>2181</port>
        </node>
        <node>
            <host>example_2_3</host>
            <port>2181</port>
        </node>
    </source_zookeper>
</auxiliary_zookeepers>
cluster('cluster_name', db.table[, sharding_key])
cluster('cluster_name', db, table[, sharding_key])
clusterAllReplicas('cluster_name', db.table[, sharding_key])
clusterAllReplicas('cluster_name', db, table[, sharding_key])
ALTER TABLE users FETCH PARTITION 201902 FROM 'source_zookeper:/clickhouse/tables/01-01/visits';
ALTER TABLE users ATTACH PARTITION 201902;
​
ALTER TABLE users FETCH PART 201901_2_2_0 FROM 'source_zookeeper:/clickhouse/tables/01-01/visits';
ALTER TABLE users ATTACH PART 201901_2_2_0;

但是:fetch part 需要clickhouse 21.3+ 的版本,集群迁移很可能源集群的版本不支持,可能需要在源集群进行升级后进行迁移

优点:

性能最佳,该过程支持远程http 拉去part ,性能基本与磁盘性能相当

缺点:

需要统计源集群所有part 的信息,可以参数 https://cloud.tencent.com/developer/article/1867322,然后组装拼接SQl 完成

且不支持按照key 维度的重分布,除非源集群和目标集群shard 数量相同

三 clickhouse copier

详情

clickhouse copier 为clickhouse 官方开源工具,本质逻辑也为insert select, 但它会将任务拆分

<yandex>
    <remote_servers>
        <source_cluster>
        </source_cluster>
        <destination_cluster>
        </destination_cluster>
    </remote_servers>
​
    <max_workers>20</max_workers>
    <number_of_splits>1</number_of_splits> <!-- 强烈迁移为1,默认为10-->
​
    <settings_pull>
        <readonly>1</readonly>
    </settings_pull>
    
    <settings_push>
        <readonly>0</readonly>
    </settings_push>
​
    <settings>
        <insert_distributed_sync>1</insert_distributed_sync>
    </settings>
​
    <tables>
        <!-- A table task, copies one table. -->
        <table_hits>
            <!-- Source cluster name (from <remote_servers/> section) and tables in it that should be copied -->
            <cluster_pull>source_cluster</cluster_pull>
            <database_pull>test</database_pull>
            <table_pull>hits</table_pull>
​
            <!-- Destination cluster name and tables in which the data should be inserted -->
            <cluster_push>destination_cluster</cluster_push>
            <database_push>test</database_push>
            <table_push>hits2</table_push>
​
            <!-- Engine of destination tables.
                 If destination tables have not be created, workers create them using columns definition from source tables and engine
                 definition from here.
​
                 NOTE: If the first worker starts insert data and detects that destination partition is not empty then the partition will
                 be dropped and refilled, take it into account if you already have some data in destination tables. You could directly
                 specify partitions that should be copied in <enabled_partitions/>, they should be in quoted format like partition column of
                 system.parts table.
            -->
            <engine>
            ENGINE=ReplicatedMergeTree('/clickhouse/tables/{cluster}/{shard}/hits2', '{replica}')
            PARTITION BY toMonday(date)
            ORDER BY (CounterID, EventDate)
            </engine>
​
            <!-- Sharding key used to insert data to destination cluster -->
            <sharding_key>jumpConsistentHash(intHash64(UserID), 2)</sharding_key>
​
            <!-- Optional expression that filter data while pull them from source servers -->
            <where_condition>CounterID != 0</where_condition>
​
            <!-- This section specifies partitions that should be copied, other partition will be ignored.
                 Partition names should have the same format as
                 partition column of system.parts table (i.e. a quoted text).
                 Since partition key of source and destination cluster could be different,
                 these partition names specify destination partitions.
​
                 NOTE: In spite of this section is optional (if it is not specified, all partitions will be copied),
                 it is strictly recommended to specify them explicitly.
                 If you already have some ready partitions on destination cluster they
                 will be removed at the start of the copying since they will be interpeted
                 as unfinished data from the previous copying!!!
            -->
            <enabled_partitions>
                <partition>'2018-02-26'</partition>
                <partition>'2018-03-05'</partition>
                ...
            </enabled_partitions>
        </table_hits>
​
        <!-- Next table to copy. It is not copied until previous table is copying. -->
        <table_visits>
        ...
        </table_visits>
        ...
    </tables>
</yandex>

操作步骤:

  1. 准备以上迁移任务 task.xml
  2. 准备zoookeeper.xml
  3. task.xml 上传到zookeeper ,这里zookeeper 为迁移任务的zookeeper,可以使用目标集群的zookeeper,也可以使用源集群的zookeeper
bin/zkCli.sh -server ip:2181   deleteall /clickhouse
bin/zkCli.sh -server ip:2181   create /clickhouse
bin/zkCli.sh -server ip:2181   deleteall /clickhouse/copier
bin/zkCli.sh -server ip:2181   create /clickhouse/copier
bin/zkCli.sh -server ip:2181   create /clickhouse/copier/table1
bin/zkCli.sh -server ip:2181   create /clickhouse/copier/table1/description "`cat task.xml`"

5. copier 任务拉起

nohup clickhouse-copier --config zookeeper.xml  --task-path /clickhouse/copier/table1   --log-level=warning --base-dir ./logs/table1 & 
nohup clickhouse-copier --config zookeeper.xml  --task-path /clickhouse/copier/table1   --log-level=warning --base-dir ./logs/table1 & 

建议:

  1. 一个task.xml 是可以配置多个表的迁移任务,但是建议大表还是要分开,copier是一张一张表来完成迁移的,即使开始多个copier 任务,所以你应该有 /clickhouse/copier/table1 /clickhouse/copier/table2 /clickhouse/copier/table...
  2. 相同表的任务你可以开启多个copier进程进行迁移,加速迁移任务,强烈建议在目标集群的每个clickhouse server 节点都拉起相同的任务 (1因为copier 性能取决于copier 所在机器的性能,多个节点,性能自然好 ,2 某种意义也是分布式的,因为多个copier 任务迁移一张表的情况下,首先会将表拆分成很多的任务,存放到zookeeeper上,然后每个copier 获取任务去执行)
  3. copier 虽然是支持partition级别的,但是我们不能为一张表开始不同task 任务,因为在copier 迁移的过程中,目标集群会创建临时表 (比如你迁移的 table1 临时表为 table1_piece_0 、table_piece_1),不同的任务使用相同的临时表,一个任务完成的时候,会讲起临时表删除,结果你可想而之,那就是剩下的任务迁移会失败 但是:这里说个但是 也有其他的绕过它的放哪,该方案建立在多副本的情况下,你可以shard1_replica1 迁移partition 1 , shard1_replica2 迁移partition2

总的task

<yandex>
    <remote_servers>
        <source_cluster>
        </source_cluster>
        <destination_cluster>
           <shard>
             <replica>1
             </replica>
             <replica>2
             </replica>
          </shard>
        </destination_cluster>
    </remote_servers>

    <tables>
        <table_hits>
            <cluster_pull>source_cluster</cluster_pull>
            <database_pull>test</database_pull>
            <table_pull>hits</table_pull>
            <cluster_push>destination_cluster</cluster_push>
            <database_push>test</database_push>
            <table_push>hits</table_push>

            <enabled_partitions>
                <partition>'partiiton1'</partition>
                <partition>'partiiton2'</partition>
            </enabled_partitions>
        </table_hits>

    </tables>
</yandex>

我们可以将 task 拆分为 task1 task2

task1.xml

<yandex>
    <remote_servers>
        <source_cluster>
        </source_cluster>
        <destination_cluster>
           <shard>
             <replica>1
             </replica>
             <!--<replica>2
             </replica>-->
          </shard>
        </destination_cluster>
    </remote_servers>

    <tables>
        <table_hits>
            <cluster_pull>source_cluster</cluster_pull>
            <database_pull>test</database_pull>
            <table_pull>hits</table_pull>
            <cluster_push>destination_cluster</cluster_push>
            <database_push>test</database_push>
            <table_push>hits</table_push>

            <enabled_partitions>
                <partition>'partiiton1'</partition>
                <!--<partition>'partiiton2'</partition>-->
            </enabled_partitions>
        </table_hits>

    </tables>
</yandex>

task2

<yandex>
    <remote_servers>
        <source_cluster>
        </source_cluster>
        <destination_cluster>
           <shard>
             <!--<replica>1
             </replica>-->
             <replica>2
             </replica>
          </shard>
        </destination_cluster>
    </remote_servers>

    <tables>
        <table_hits>
            <cluster_pull>source_cluster</cluster_pull>
            <database_pull>test</database_pull>
            <table_pull>hits</table_pull>
            <cluster_push>destination_cluster</cluster_push>
            <database_push>test</database_push>
            <table_push>hits</table_push>

            <enabled_partitions>
                <!--<partition>'partiiton1'</partition>-->
                <partition>'partiiton2'</partition>
            </enabled_partitions>
        </table_hits>

    </tables>
</yandex>

4. 如果源集群与目标集群的节点,结构完全一致的情况,强烈建议点对点迁移,shard 对shard ,性能提升明显,且将对应的copier 任务放到对应的节点执行 原先的task.xml

<yandex>
    <remote_servers>
        <source_cluster>
           <shard>1
          </shard>
           <shard>2
          </shard>
        </source_cluster>
        <destination_cluster>
           <shard>1
          </shard>
           <shard>2
          </shard>
        </destination_cluster>
    </remote_servers>

task1

<yandex>
    <remote_servers>
        <source_cluster>
           <shard>1
          </shard>
          <!-- <shard>2
          </shard>-->
        </source_cluster>
        <destination_cluster>
           <shard>1
          </shard>
           <!--<shard>2
          </shard>-->
        </destination_cluster>
    </remote_servers>

task2

<yandex>
    <remote_servers>
        <source_cluster>
          <!-- <shard>1
          </shard>-->
           <shard>2
          </shard>
        </source_cluster>
        <destination_cluster>
          <!-- <shard>1
          </shard>-->
           <shard>2
          </shard>
        </destination_cluster>
    </remote_servers>

5. 最佳时间就是 3+4 组合

优点:稳定

缺点:使用复杂

希望可以给大家带来帮助,欢迎大家使用 腾讯云clickhouse https://cloud.tencent.com/product/cdwch

马上也会推出弹性版ClikHouse https://mp.weixin.qq.com/s/dxoU7S7hOK_PIBZp5OZTKA

原文地址:https://cloud.tencent.com/developer/article/2109528

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


学习编程是顺着互联网的发展潮流,是一件好事。新手如何学习编程?其实不难,不过在学习编程之前你得先了解你的目的是什么?这个很重要,因为目的决定你的发展方向、决定你的发展速度。
IT行业是什么工作做什么?IT行业的工作有:产品策划类、页面设计类、前端与移动、开发与测试、营销推广类、数据运营类、运营维护类、游戏相关类等,根据不同的分类下面有细分了不同的岗位。
女生学Java好就业吗?女生适合学Java编程吗?目前有不少女生学习Java开发,但要结合自身的情况,先了解自己适不适合去学习Java,不要盲目的选择不适合自己的Java培训班进行学习。只要肯下功夫钻研,多看、多想、多练
Can’t connect to local MySQL server through socket \'/var/lib/mysql/mysql.sock问题 1.进入mysql路径
oracle基本命令 一、登录操作 1.管理员登录 # 管理员登录 sqlplus / as sysdba 2.普通用户登录
一、背景 因为项目中需要通北京网络,所以需要连vpn,但是服务器有时候会断掉,所以写个shell脚本每五分钟去判断是否连接,于是就有下面的shell脚本。
BETWEEN 操作符选取介于两个值之间的数据范围内的值。这些值可以是数值、文本或者日期。
假如你已经使用过苹果开发者中心上架app,你肯定知道在苹果开发者中心的web界面,无法直接提交ipa文件,而是需要使用第三方工具,将ipa文件上传到构建版本,开...
下面的 SQL 语句指定了两个别名,一个是 name 列的别名,一个是 country 列的别名。**提示:**如果列名称包含空格,要求使用双引号或方括号:
在使用H5混合开发的app打包后,需要将ipa文件上传到appstore进行发布,就需要去苹果开发者中心进行发布。​
+----+--------------+---------------------------+-------+---------+
数组的声明并不是声明一个个单独的变量,比如 number0、number1、...、number99,而是声明一个数组变量,比如 numbers,然后使用 nu...
第一步:到appuploader官网下载辅助工具和iCloud驱动,使用前面创建的AppID登录。
如需删除表中的列,请使用下面的语法(请注意,某些数据库系统不允许这种在数据库表中删除列的方式):
前不久在制作win11pe,制作了一版,1.26GB,太大了,不满意,想再裁剪下,发现这次dism mount正常,commit或discard巨慢,以前都很快...
赛门铁克各个版本概览:https://knowledge.broadcom.com/external/article?legacyId=tech163829
实测Python 3.6.6用pip 21.3.1,再高就报错了,Python 3.10.7用pip 22.3.1是可以的
Broadcom Corporation (博通公司,股票代号AVGO)是全球领先的有线和无线通信半导体公司。其产品实现向家庭、 办公室和移动环境以及在这些环境...
发现个问题,server2016上安装了c4d这些版本,低版本的正常显示窗格,但红色圈出的高版本c4d打开后不显示窗格,
TAT:https://cloud.tencent.com/document/product/1340