CK分布式DDL语法使用

一、复制表

1、基本语法

1)ReplicatedMergeTree 基本语法

-- 每个replica下执行
create table ${table_name} (xx,xx,xx)
engine = ReplicatedMergeTree('${zk_path}','${replica_name}') order by xx
  • zk_path : 指定在zk中创建数据表的路径,配置模版为 /clickhouse/tables/ s h a r d / {shard}/ shard/{table_name},{shard}为分片编号、${table_name}为数据表名称
  • replica_name : 副本名称
  • 同一分片下不同副本的数据表,应该定义相同的zk_path、不同的replica_name
  • 不同分片下不同副本的数据表,应该定义不同的zk_path、不同的replica_name

2)分布式DDL语法

create table ${table_name} on cluster ${cluster_name}(xx,xx,xx)
engine = ReplicatedMergeTree('/clickhouse/tables/{layer}-{shard}/${table_name}','{replica}') order by xx
  • 基本语法与非分布式语法基本一致
  • zk_path中,{layer}、{shard}、{replica}都是macros设置的参数值,不需要使用当前实际属性值进行替换,其余参数根据实际属性值设置。

2、示例

1)0分片多副本模式创建表

表数据创建:

-- shard1-repl1,创建t1的复制表
-- /clickhouse/tables/01-01/image_label : 01-01 通过 集群标识-分片标识确定
-- cluster01-01-1   : 集群标识-分片标识:副本标识
sdw1 :) create database db2;
sdw1 :) CREATE TABLE t1(`id` Int32,`name` String) ENGINE = ReplicatedMergeTree('/clickhouse/tables/01-01/t1','cluster01-01-1') ORDER BY id ;

-- shard1-repl2,创建t1的复制表
sdw2 :) create database db2;
sdw2 :) CREATE TABLE t1(`id` Int32,`name` String) ENGINE = ReplicatedMergeTree('/clickhouse/tables/01-01/t1','cluster01-01-2') ORDER BY id ;

数据写入:

-- shard1-repl1
sdw1 :) insert into t1 values(1,'aa'),(2,'bb'),(3,'cc');

-- shard1-repl2
sdw2 :) insert into t1 values(4,'dd');
sdw2 :) insert into t1 values(5,'ee');

数据查询:

-- shard1-repl1
sdw1 :) select * from t1;

SELECT *
FROM t1

┌─id─┬─name─┐
│  1 │ aa   │
│  2 │ bb   │
│  3 │ cc   │
└────┴──────┘
┌─id─┬─name─┐
│  4 │ dd   │
└────┴──────┘
┌─id─┬─name─┐
│  5 │ ee   │
└────┴──────┘

5 rows in set. Elapsed: 0.009 sec.

-- shard1-repl2
sdw2 :) select * from t1;

SELECT *
FROM t1

┌─id─┬─name─┐
│  5 │ ee   │
└────┴──────┘
┌─id─┬─name─┐
│  4 │ dd   │
└────┴──────┘
┌─id─┬─name─┐
│  1 │ aa   │
│  2 │ bb   │
│  3 │ cc   │
└────┴──────┘

5 rows in set. Elapsed: 0.008 sec.

2)分布式DDL创建语句

create table t4 on cluster shard1_repl1 (`id` Int32,`name` String) engine=ReplicatedMergeTree('/clickhouse/tables/{shard}/t4','{replica}') order by id;

二、分布式表

1、基本语法

1)分布表基本语法

-- 每个shard下执行
- local表
CREATE TABLE ${table_name}_local (`id` Int32,`name` String) 
ENGINE = ReplicatedMergeTree('${path}','${replica}') ORDER BY id ;
- all表
CREATE TABLE ${table_name}_all (`id` Int32,`name` String) 
ENGINE = Distributed(${cluster_name},${db_name},${table_name}_local[,sharding_key])
  • cluster_name : 集群名字,可由metrika.xml配置文件中的<clickhouse_remote_servers>查看
  • db_name、table_name : 分布式表所对应的数据库以及local表名(做distribute表和local表的映射关系)
  • sharding_key : 分片键,选填参数

2)分布式DDL语法

- local表
create table ${table_name}_local on cluster ${cluster_name} (`id` Int32,`name` String) 
engine=ReplicatedMergeTree('/clickhouse/tables/{layer}-{shard}/${table_name}','{replica}') order by id;
- all表
create table ${table_name}_all on cluster ${cluster_name} (`id` Int32,`name` String) 
engine=Distributed(${cluster_name},${db_name},${table_name}_local[,sharding_key]);

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bAhn3WI9-1613744797964)(http://note.youdao.com/yws/res/73825/F1C3A6F834314F67B132FE3E6EA811F5)]

2、示例

1)2分片0副本模式创建分布式表

表数据创建:

-- shard1
sdw1 :) CREATE TABLE t3(`id` Int32,`name` String) ENGINE = ReplicatedMergeTree('/clickhouse/tables/02-01/t3','cluster02-01') ORDER BY id ;
sdw1 :) CREATE TABLE t3_all(`id` Int32,`name` String) ENGINE = Distributed(shard2_repl0,db2,t3,rand())

-- shard2
sdw2 :) CREATE TABLE t3(`id` Int32,`name` String) ENGINE = ReplicatedMergeTree('/clickhouse/tables/02-02/t3','cluster02-02') ORDER BY id ;
sdw2 :) CREATE TABLE t3_all(`id` Int32,`name` String) ENGINE = Distributed(shard2_repl0,db2,t3,rand())

数据写入:

-- shard1
sdw1 :) insert into t3 values(1,'aa'),(2,'bb'),(3,'cc');

-- shard2
sdw2 :) insert into t3 values(4,'dd'),(5,'ee'),(6,'ff');

数据查询:

-- shard1
sdw1 :) select * from t3;

SELECT *
FROM t3

┌─id─┬─name─┐
│  1 │ aa   │
│  2 │ bb   │
│  3 │ cc   │
└────┴──────┘

3 rows in set. Elapsed: 0.006 sec.

sdw1 :) select * from t3_all;

SELECT *
FROM t3_all

┌─id─┬─name─┐
│  1 │ aa   │
│  2 │ bb   │
│  3 │ cc   │
└────┴──────┘
┌─id─┬─name─┐
│  4 │ dd   │
│  5 │ ee   │
│  6 │ ff   │
└────┴──────┘

6 rows in set. Elapsed: 0.019 sec.

-- shard2
sdw2 :) select * from t3;

SELECT *
FROM t3

┌─id─┬─name─┐
│  4 │ dd   │
│  5 │ ee   │
│  6 │ ff   │
└────┴──────┘

3 rows in set. Elapsed: 0.006 sec.

sdw2 :) select * from t3_all;

SELECT *
FROM t3_all

┌─id─┬─name─┐
│  4 │ dd   │
│  5 │ ee   │
│  6 │ ff   │
└────┴──────┘
┌─id─┬─name─┐
│  1 │ aa   │
│  2 │ bb   │
│  3 │ cc   │
└────┴──────┘

6 rows in set. Elapsed: 0.017 sec.

2)分布式DDL创建语法

create table t7_all on cluster shard2_repl0 (`id` Int32,`name` String) 
engine=Distributed(shard2_repl0,db1,t7_local,rand());
create table t7_local on cluster shard2_repl0 (`id` Int32,`name` String) 
engine=ReplicatedMergeTree('/clickhouse/tables/{shard}/t7_local','{replica}') order by id;

猜你喜欢

转载自blog.csdn.net/weixin_37692493/article/details/113873197