|NO.Z.00027|——————————|BigDataEnd|——|Hadoop&OLAP_ClickHouse.V08|——|ClickHouse.v08|ClickHouse:ClickHouse副本分片|Distributed用法|

一、Distributed用法
### --- Distributed表引擎

~~~     Distributed表引擎:
~~~     all : 全局查询的
~~~     local:真正的保存数据的表
### --- Distributed

~~~     分布式引擎,本身不存储数据, 但可以在多个服务器上进行分布式查询。 
~~~     读是自动并行的。读取时,远程服务器表的索引(如果有的话)会被使用。
~~~     Distributed(cluster_name, database, table [, sharding_key])
~~~     # 参数解析:

~~~     cluster_name - 服务器配置文件中的集群名,在/etc/metrika.xml中配置的
~~~     database – 数据库名
~~~     table – 表名
~~~     sharding_key – 数据分片键
二、Distributed案例演示:
### --- 在hadoop01、hadoop02、hadooo03上分别创建一个表t

~~~     # 在三台主机创建t表
hadoop01 :) create table t(id UInt16, name String) ENGINE=TinyLog;
### --- 在三台机器的t表中插入一些数据

~~~     # 在三台主机t表插入数据
hadoop01 :) insert into t(id, name) values (1, 'zhangsan');
hadoop01 :) insert into t(id, name) values (2, 'lisi');
### --- 在hadoop01上创建分布式表

hadoop01 :) create table dis_table(id UInt16, name String)
            ENGINE=Distributed(perftest_3shards_1replicas, default, t, id);
~~~输出参数
### --- 往dis_table中插入数据

hadoop01 :) insert into dis_table select * from t;
### --- 查看数据量

~~~     # 可以看到每个节点大约有1/3的数据
hadoop01 :) select count() from dis_table;
┌─count()─┐
│ 8       │
└─────────┘
hadoop01 :) select count() from t;
┌─count()─┐
│ 3       │
└─────────┘
三、Distributed案例演示二:
### --- 创建分布式全局表

~~~     # 创建全局分布式表:
hadoop01 :) create table distributed_all 
            on cluster perftest_3shards_1replicas (id UInt32)
            engine=Distributed(perftest_3shards_1replicas,'default',distributed_local,rand());
~~~输出参数
ENGINE = Distributed(perftest_3shards_1replicas, 'default', distributed_local, rand())

┌─host─────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ hadoop02 │ 9000 │      0 │       │                   2 │                2 │
└──────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
┌─host─────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ hadoop01 │ 9000 │      0 │       │                   1 │                2 │
└──────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
┌─host─────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ hadoop03 │ 9000 │      0 │       │                   0 │                2 │
└──────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
### --- 创建分布式引擎表

~~~     # 创建引擎分布式表
hadoop01 :) create table distributed_local
            on cluster perftest_3shards_1replicas (id UInt32)
            engine=MergeTree() order by id partition by id;
~~~ 输出参数
┌─host─────┬─port─┬─status─┬─error─┬─num_hosts_remaining─┬─num_hosts_active─┐
│ hadoop02 │ 9000 │      0 │       │                   2 │                0 │
│ hadoop01 │ 9000 │      0 │       │                   1 │                0 │
│ hadoop03 │ 9000 │      0 │       │                   0 │                0 │
└──────────┴──────┴────────┴───────┴─────────────────────┴──────────────────┘
### --- 插入数据

~~~     # 在三台主机上插入数据,查看数据是否实现分布式效果
hadoop01 :) show tables;
┌─name───────────────┐
│ distributed_all    │
│ distributed_local  │
└────────────────────┘
hadoop02 :) show tables;
┌─name──────────────┐
│ distributed_all   │
│ distributed_local │
└───────────────────┘
hadoop03 :) show tables;
┌─name──────────────┐
│ distributed_all   │
│ distributed_local │
└───────────────────┘
### --- 效验结果

~~~     # 在hadoop01上distributed_all中插入数据
hadoop01 :) insert into distributed_all values(1);
~~~     # 在hadoop01上distributed_all中查看插入的数据
hadoop01 :) select * from distributed_all;
┌─id──────────────┐
│  1              │
└─────────────────┘
~~~     # 在三台主机上查看到distributed_local
~~~     只有一个副本保存到hadoop03主机下,这种机制是我们实现的结果:
~~~     将数据保存在集群的某一个节点下
~~~     可以看到每个节点大约有1/3的数据

hadoop01 :) select * from distributed_local;
hadoop02 :) select * from distributed_local;
hadoop03 :) select * from distributed_local;
┌─id──────────────┐
│  1              │
└─────────────────┘

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on 2022-04-14 14:09  yanqi_vip  阅读(27)  评论(0)    收藏  举报

导航