Clickhouse Distributed分布式表引擎的基本介绍和使用说明

Clickhouse分布式表操作与数据分片策略
本文详细介绍了Clickhouse中分布式表的创建、更改、删除以及增删改查操作,包括如何创建和管理分布式本地表,并探讨了数据写入时的分片规则。通过示例展示了如何使用rand()作为分片策略。此外,还讨论了分布式表的更新和删除操作的异步特性及其对数据的影响。

Clickhouse的集群部署可以参考我的Clickhouse版本21.6.5.37的分片和副本分布式安装

Distributed表需要和其它表引擎一起使用,本身不储存数据,只是作分布式本地表的一层透明代理

1. 分布式的本地表

1.1 分布式的创建本地表

clickhouse1 :) 
clickhouse1 :) create table distribute_test_local on cluster sharding_ha(
:-] id UInt64,
:-] name String
:-] ) engine = ReplicatedMergeTree('/clickhouse/tables/distribute_test/{shard}', '{replica}')
:-] order by id;

CREATE TABLE distribute_test_local ON CLUSTER sharding_ha
(
    `id` UInt64,
    `name` String
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/distribute_test/{shard}', '{replica}')
ORDER BY id

Query id: 23f5ef8b-02e5-4b34-b871-208cc4983325

┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse2 │ 9000 │      0 │       │                   3 │                0 │
│ clickhouse3 │ 9000 │      0 │       │                   2 │                0 │
│ clickhouse1 │ 9000 │      0 │       │                   1 │                0 │
│ clickhouse4 │ 9000 │      0 │       │                   0 │                0 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘

4 rows in set. Elapsed: 1.098 sec. 

clickhouse1 :) 


这里我们用{shard}{replica}两个动态宏变量代替了硬编码

1.2 分布式的更改本地表表名

clickhouse1 :) 
clickhouse1 :) rename table distribute_test_local to distribute_test_local2 on cluster sharding_ha;

RENAME TABLE distribute_test_local TO distribute_test_local2 ON CLUSTER sharding_ha

Query id: 06884b49-2627-4b1a-a8d2-6df860a9cd8f

┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse3 │ 9000 │      0 │       │                   3 │                0 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse2 │ 9000 │      0 │       │                   2 │                0 │
│ clickhouse1 │ 9000 │      0 │       │                   1 │                0 │
│ clickhouse4 │ 9000 │      0 │       │                   0 │                0 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘

4 rows in set. Elapsed: 0.121 sec. 

clickhouse1 :)
clickhouse1 :) rename table distribute_test_local2 to distribute_test_local on cluster sharding_ha;

RENAME TABLE distribute_test_local2 TO distribute_test_local ON CLUSTER sharding_ha

Query id: 265a5678-f51e-4bad-8b91-91ea220bcd2b

┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse3 │ 9000 │      0 │       │                   3 │                0 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse2 │ 9000 │      0 │       │                   2 │                0 │
│ clickhouse1 │ 9000 │      0 │       │                   1 │                0 │
│ clickhouse4 │ 9000 │      0 │       │                   0 │                0 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘

4 rows in set. Elapsed: 0.085 sec. 

clickhouse1 :)

1.3 分布式的删除本地表

clickhouse1 :)
clickhouse1 :) drop table distribute_test_local on cluster sharding_ha;

DROP TABLE distribute_test_local ON CLUSTER sharding_ha

Query id: 61540ac6-654b-4319-98aa-3462bac7bbeb

┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse1 │ 9000 │      0 │       │                   3 │                0 │
│ clickhouse3 │ 9000 │      0 │       │                   2 │                0 │
│ clickhouse4 │ 9000 │      0 │       │                   1 │                0 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse2 │ 9000 │      0 │       │                   0 │                0 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘

4 rows in set. Elapsed: 0.247 sec. 

clickhouse1 :) 

2. Distributed表

2.1 创建Distributed表

clickhouse1 :) 
clickhouse1 :) create table distribute_test_all on cluster sharding_ha(
:-] id UInt64,
:-] name String
:-] ) engine = Distributed(sharding_ha, default, distribute_test_local, rand());

CREATE TABLE distribute_test_all ON CLUSTER sharding_ha
(
    `id` UInt64,
    `name` String
)
ENGINE = Distributed(sharding_ha, default, distribute_test_local, rand())

Query id: 8bbd66c1-e1cc-44f3-b860-a2a892c718fa

┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse2 │ 9000 │      0 │       │                   3 │                2 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse3 │ 9000 │      0 │       │                   2 │                0 │
│ clickhouse1 │ 9000 │      0 │       │                   1 │                0 │
│ clickhouse4 │ 9000 │      0 │       │                   0 │                0 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘

4 rows in set. Elapsed: 0.300 sec. 

clickhouse1 :) 

说明:

  • 这里数据写入时的分片规则我们采用了rand()随机的模式,具体可以参考本文的第4部分
  • Distributed采用读时检测机制,对分布式表和本地表的创建顺序无要求
  • 会在集群的所有节点上创建一张Distributed表,所以从每个节点执行读写请求都是一样的

2.2 删除分布式表

clickhouse1 :)
clickhouse1 :) drop table distribute_test_all on cluster sharding_ha;

DROP TABLE distribute_test_all ON CLUSTER sharding_ha

Query id: ac692e90-472b-447e-9908-e4d79fa7b93a

┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse1 │ 9000 │      0 │       │                   3 │                0 │
│ clickhouse3 │ 9000 │      0 │       │                   2 │                0 │
│ clickhouse4 │ 9000 │      0 │       │                   1 │                0 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse2 │ 9000 │      0 │       │                   0 │                0 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘

4 rows in set. Elapsed: 0.263 sec. 

clickhouse1 :)

2.3 Distributed表其它语法

Distributed表的RENAME和ALTER,其中ALTER并不包括分区的操作(ATTACH PARTITION、REPLACE PARTITION等),这些语法只作用Distributed表本身,不会作用于本地表

3. Distributed表的增删改查

Distributed表支持insert、select,不支持mutation、delete,但可以用分布式操作本地表的方式来实现

3.1 insert

clickhouse1 :) 
clickhouse1 :) insert into distribute_test_all(id, name) values(1, '一');

INSERT INTO distribute_test_all (id, name) VALUES

Query id: e1bd2e12-1055-4095-b971-c7494c99d819

Ok.

1 rows in set. Elapsed: 0.069 sec. 

clickhouse1 :)
  • 一个Block数据块的大小由max_insert_block_size(默认1048576)控制,当一条insert语句插入的数据量小于max_insert_block_size,则insert插入的数据要么全部成功,要么全部失败;但此原则不适用于clickhouse-client命令行操作或insert select子句

3.2 select

clickhouse1 :)
clickhouse1 :) select * from distribute_test_all;

SELECT *
FROM distribute_test_all

Query id: 2097dcf0-d204-4670-af5c-9c221eec82d1

┌─id─┬─name─┐
│  1 │ 一   │
└────┴──────┘

1 rows in set. Elapsed: 0.041 sec. 

clickhouse1 :) 


3.3 分布式本地表mutation

  • update和delete命令是一个异步的操作,提交后,客户端不会收到是否成功的消息
  • 可以通过system.mutations的is_done字段查看update和delete是否执行成功
  • 只有等partition分区合并的时候,才会将不要的旧数据目录删除

3.3.1 分布式本地表update

clickhouse1 :)
clickhouse1 :) alter table distribute_test_local on cluster sharding_ha update name = 'yi' where id = 1;

ALTER TABLE distribute_test_local ON CLUSTER sharding_ha
    UPDATE name = 'yi' WHERE id = 1

Query id: 1ee6903f-6816-4fb0-9b2c-049b612f4382

┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse2 │ 9000 │      0 │       │                   3 │                0 │
│ clickhouse1 │ 9000 │      0 │       │                   2 │                0 │
│ clickhouse3 │ 9000 │      0 │       │                   1 │                0 │
│ clickhouse4 │ 9000 │      0 │       │                   0 │                0 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘

4 rows in set. Elapsed: 0.175 sec. 

clickhouse1 :)
  • primary key、partition by字段不能被update

3.3.2 分布式本地表delete

clickhouse1 :)
clickhouse1 :) alter table distribute_test_local on cluster sharding_ha delete where id = 1;

ALTER TABLE distribute_test_local ON CLUSTER sharding_ha
    DELETE WHERE id = 1

Query id: 64865460-0c0b-404b-b922-d02af2faefff

┌─host────────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ clickhouse2 │ 9000 │      0 │       │                   3 │                0 │
│ clickhouse3 │ 9000 │      0 │       │                   2 │                0 │
│ clickhouse1 │ 9000 │      0 │       │                   1 │                0 │
│ clickhouse4 │ 9000 │      0 │       │                   0 │                0 │
└─────────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘

4 rows in set. Elapsed: 0.342 sec. 

clickhouse1 :)

4. 数据写入时的分片规则

在本文的2.1部分,我们采用的分片规则是rand(),分片键可以是Int或UInt类型,如userid, intHash64(userid); weight是整数类型,越大则落入的数据越多

集群分片和权重如下:

服务器分片weight落入此分片取模的值范围
clickhouse10110 - 0
clickhouse30211 - 1

假设rand()返回的值为66, 则 取 模 的 值 = 66 % ( 1 + 1 ) = 0 取模的值 = 66\%(1 + 1) = 0 =66%(1+1)=0, 落入分片01

ClickHouse 中创建分布式需要使用 `Distributed` 表引擎,它本身不存储数据,而是作为本地的代理,将数据分布到集群中的多个节点上。创建分布式通常需要以下步骤: ### 创建本地(如 `MergeTree` 系列表引擎分布式需要与本地(存储数据的)一起工作。通常会在每个节点上创建相同的本地结构。 例如,创建一个使用 `MergeTree` 的本地: ```sql CREATE TABLE local_table_name ON CLUSTER cluster_name ( column1 UInt32, column2 String, column3 Date ) ENGINE = MergeTree() ORDER BY (column1, column2); ``` 其中: - `ON CLUSTER cluster_name` 指定在集群上创建。 - `local_table_name` 是本地的名称。 - `MergeTree()` 是常见的存储引擎。 - `ORDER BY` 指定排序键。 ### 创建分布式 创建分布式时,使用 `Distributed` 引擎,并指定集群名称、数据库名称、本地名称分片键(可选)。 ```sql CREATE TABLE distributed_table_name ( column1 UInt32, column2 String, column3 Date ) ENGINE = Distributed(cluster_name, database_name, local_table_name, shard_key); ``` 其中: - `distributed_table_name` 是分布式的名称。 - `cluster_name` 是集群的名称。 - `database_name` 是本地所在的数据库名称。 - `local_table_name` 是本地的名称。 - `shard_key` 是分片键,用于决定数据如何分布在各个分片上(可选)。如果不指定,则使用默认的分片策略。 ### 插入数据 插入数据时,通常会将数据写入分布式,它会自动将数据路由到正确的分片。 ```sql INSERT INTO distributed_table_name (column1, column2, column3) VALUES (1, 'example', '2024-01-01'); ``` ### 查询数据 查询分布式时,它会从所有分片中收集数据并返回聚合结果。 ```sql SELECT * FROM distributed_table_name WHERE column1 = 1; ``` ### 注意事项 1. **集群配置**:确保在 `config.xml` 或 `configs.d/` 中正确配置了集群信息,包括分片副本的定义。 2. **分片键**:选择合适的分片键可以优化数据分布查询性能[^3]。 3. **数据一致性**:分布式本身不提供数据一致性保障,需要依赖底层本地集群配置。 4. **性能优化**:如果数据写入频率较高,建议使用批量插入以减少网络开销。 ### 示例 假设集群名称为 `my_cluster`,数据库名称为 `default`,本地名称为 `local_table`,分布式名称为 `distributed_table`。 创建本地: ```sql CREATE TABLE local_table ON CLUSTER my_cluster ( id UInt32, name String, date Date ) ENGINE = MergeTree() ORDER BY (id, name); ``` 创建分布式: ```sql CREATE TABLE distributed_table ( id UInt32, name String, date Date ) ENGINE = Distributed(my_cluster, default, local_table, id); ``` 插入数据: ```sql INSERT INTO distributed_table (id, name, date) VALUES (1, 'Alice', '2024-01-01'); ``` 查询数据: ```sql SELECT * FROM distributed_table WHERE id = 1; ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值