clickhouse三分片一副本集群部署

个人学习笔记,谢绝转载!!! 原文:https://www.cnblogs.com/wshenjin/p/13140191.html


简单分布式MergeTree + Distributed,三分片一副本

节点IP

  • 192.168.31.101
  • 192.168.31.102
  • 192.168.31.103

###部署clickhouse集群 三个节点的安装省略

config.xml的一些配置:

    <!-- Path to data directory, with trailing slash. -->
    <path>/data/database/clickhouse/</path>
    <!-- Path to temporary data for processing hard queries. -->
    <tmp_path>/data/database/clickhouse/tmp/</tmp_path>
    <!-- Directory with user provided files that are accessible by 'file' table function. -->
    <user_files_path>/data/database/clickhouse/user_files/</user_files_path>
    <!-- Directory in <clickhouse-path> containing schema files for various input formats.The directory will be created if it doesn't exist.-->
    <format_schema_path>/data/database/clickhouse/format_schemas/</format_schema_path>
    <!-- Same for hosts with disabled ipv6.-->
    <listen_host>0.0.0.0</listen_host> 
    <timezone>Asia/Shanghai</timezone> 
    <!-- 集群相关的配置,可以用外部依赖文件来配置,因此这里留空即可 -->
    <remote_servers incl="clickhouse_remote_servers" > </remote_servers>
    <!-- 外部依赖配置文件 -->
    <include_from>/etc/clickhouse-server/metrika.xml</include_from>

配置metrika.xml:

<yandex>
    <!-- 集群配置 -->
    <clickhouse_remote_servers>
        <!-- 集群名称 三分配一副本,名称可自定义 -->
        <ckcluster_3shards_1replicas>
            <!-- 数据分片1  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>192.168.31.101</host>
                    <port>9000</port>
                </replica>
            </shard>
            <!-- 数据分片2  -->
            <shard>
                <replica>
                <internal_replication>true</internal_replication>
                    <host>192.168.31.102</host>
                    <port>9000</port>
                </replica>
            </shard>
            <!-- 数据分片3  -->
            <shard>
                <internal_replication>true</internal_replication>
                <replica>
                    <host>192.168.31.103</host>
                    <port>9000</port>
                </replica>
            </shard>
        </ckcluster_3shards_1replicas>
    </clickhouse_remote_servers>
<!--压缩算法->
    <clickhouse_compression>
        <case>
            <min_part_size>10000000000</min_part_size>
            <min_part_size_ratio>0.01</min_part_size_ratio>
            <method>lz4</method>
        </case>
    </clickhouse_compression>
</yandex>

说明:

  • clickhouse_remote_servers与config.xml中的incl属性值对应
  • cluster_with_replica是集群名,可以自定义。
  • shard即为数据分片
  • internal_replication =true 这个参数和数据的写入,自动复制相关。从生产环境角度考虑,我们都是复制表,通过本地表写入,这里配置true就好。不推荐也不需要考虑其他情况。
  • clickhouse_compression数据的压缩。

各个节点重启后登陆查看:

SELECT *
FROM system.clusters

┌─cluster─────────────────────┬─shard_num─┬─shard_weight─┬─replica_num─┬─host_name──────┬─host_address───┬─port─┬─is_local─┬─user────┬─default_database─┬─errors_count─┬─estimated_recovery_time─┐
│ ckcluster_3shards_1replicas │         1 │            1 │           1 │ 192.168.31.101 │ 192.168.31.101 │ 9000 │        1 │ default │                  │            0 │                       0 │
│ ckcluster_3shards_1replicas │         2 │            1 │           1 │ 192.168.31.102 │ 192.168.31.102 │ 9000 │        0 │ default │                  │            0 │                       0 │
│ ckcluster_3shards_1replicas │         3 │            1 │           1 │ 192.168.31.103 │ 192.168.31.103 │ 9000 │        0 │ default │                  │            0 │                       0 │
└─────────────────────────────┴───────────┴──────────────┴─────────────┴────────────────┴────────────────┴──────┴──────────┴─────────┴──────────────────┴──────────────┴─────────────────────────┘

这样集群就搭建好了

建库建表

在各个节点建库、本地表

:) create database testdb ;
:) create table person_local (ID Int8, Name String, BirthDate Date) ENGINE = MergeTree(BirthDate, (Name, BirthDate), 8192);

在各个节点建分布表

:) create table person_all as person_local ENGINE = Distributed(ckcluster_3shards_1replicas, testdb, person_local, rand());

分布表(Distributed)本身不存储数据,相当于路由,需要指定集群名、数据库名、数据表名、分片KEY. 这里分片用rand()函数,表示随机分片。 查询分布表,会根据集群配置信息,路由到具体的数据表,再把结果进行合并。

  • person_local 为本地表,数据只是在本地
  • person_all 为分布式表,查询这个表,引擎自动把整个集群数据计算后返回

插入数据,再来查看各个节点的数据量对比

#导入3w的数据量
[root ~]# wc -l /tmp/a.csv  
30000 /tmp/a.csv
[root ~]# clickhouse-client  --host 127.0.0.1 --database testdb  --query="insert into person_all FORMAT CSV"  < /tmp/a.csv 

#对比总表和本地表的数据量
ck1 :) select count(*) from person_all ;
┌─count()─┐
│   30000 │
└─────────┘
ck1 :) select count(*) from person_local ;
┌─count()─┐
│   10092 │
└─────────┘
原文地址:https://www.cnblogs.com/wshenjin/p/13140191.html