|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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」