ClickHouse的基础概念和环境部署,可以参考我之前的文章:列式数据库管理系统——ClickHouse(version:22.7.1 环境部署)
ClickHouse有2类解析器:完整SQL解析器(递归式解析器),以及数据格式解析器(快速流式解析器) 除了 INSERT 查询,其它情况下仅使用完整SQL解析器。 官方文档:https://clickhouse.com/docs/zh/sql-reference/syntax
【语法】
`CREATE DATABASE [IF NOT EXISTS] db_name [ON CLUSTER cluster] [ENGINE = engine(...)]`
# 登录clickhouse-client -h local-168-182-111 -d default -m -u default --password 123456# 不指定数据库引擎,默认使用Atomic引擎,不指定集群,则只在当前击节点有效,其它节点是不会有这个库的create database if not exists ck_test;
默认的数据库是磁盘上的一个文件目录,执行创建之后可以在数据目录下创建文件:
ls -l /var/lib/clickhouse/data/# 可以看相关的元数据ls -l /var/lib/clickhouse/metadata# 查看数据库引擎cat /var/lib/clickhouse/metadata/ck_test.sql
# 登录clickhouse-client -h local-168-182-111 -d default -m -u default --password 123456# 查看show databases;# 查看创建数据库的语句show create database ck_test\G;# 删除数据库drop database ck_test;# 检查show databases;
# 登录clickhouse-client -h local-168-182-111 -d default -m -u default --password 123456# 查看集群select * from system.clusters;# 先查看show databases;# 不指定数据库引擎,默认使用Atomic引擎,指定集群create database if not exists ck_test ON CLUSTER ck_cluster_2022;# 检查show databases;
【语法】
CREATE DATABASE [IF NOT EXISTS] db_name [ON CLUSTER cluster]ENGINE = MySQL('host:port', ['database' | database], 'user', 'password')
引擎参数
mysql -uroot -p -h local-168-182-113密码:123456# 创建数据库create database ck_test001;USE ck_test001;CREATE TABLE `ck_test001`.`mysql_table` (`int_id` INT NOT NULL AUTO_INCREMENT,`float` FLOAT NOT NULL,PRIMARY KEY (`int_id`));show tabels;# 添加数据insert into mysql_table (`int_id`, `float`) VALUES (1,2);# 查看数据select * from mysql_table;
ClickHouse操作
# 登录clickhouse-client -h local-168-182-111 -d default -m -u default --password 123456# 创建数据库使用mysql引擎create database if not exists mysql_db ON CLUSTER ck_cluster_2022 ENGINE = MySQL('local-168-182-113:3306', 'ck_test001', 'root', '123456');
ClickHouse默认的表引擎是MergeTree,更多表引擎,可以参考官方文档,这里会讲主要的几种:MergeTree、MYSQL、HDFS、Hive、KAFKA。
【语法】
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]( name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1] [TTL expr1], name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2] [TTL expr2], ... INDEX index_name1 expr1 TYPE type1(...) GRANULARITY value1, INDEX index_name2 expr2 TYPE type2(...) GRANULARITY value2) ENGINE = MergeTree()ORDER BY expr[PARTITION BY expr][PRIMARY KEY expr][SAMPLE BY expr][TTL expr [DELETE|TO DISK 'xxx'|TO VOLUME 'xxx'], ...][SETTINGS name=value, ...]
参数解释
要按月分区,可以使用表达式 toYYYYMM(date_column) ,这里的 date_column 是一个 Date 类型的列。分区名的格式会是 "YYYYMM" 。
默认情况下主键跟排序键(由 ORDER BY 子句指定)相同。 因此,大部分情况下不需要再专门指定一个 PRIMARY KEY 子句。
如果要用抽样表达式,主键中必须包含这个表达式。例如: SAMPLE BY intHash32(UserID) ORDER BY (CounterID, EventDate, intHash32(UserID)) 。
# 表达式中必须存在至少一个 Date 或 DateTime 类型的列,比如:TTL date + INTERVAl 1 DAY
示例配置
ENGINE MergeTree() PARTITION BY toYYYYMM(EventDate) ORDER BY (CounterID, EventDate, intHash32(UserID)) SAMPLE BY intHash32(UserID) SETTINGS index_granularity=8192
同时我们设置了一个按用户 ID 哈希的抽样表达式。这使得您可以对该表中每个 CounterID 和 EventDate 的数据伪随机分布。如果您在查询时指定了 SAMPLE 子句。 ClickHouse会返回对于用户子集的一个均匀的伪随机数据采样。index_granularity 可省略因为 8192 是默认设置 。
-- 创建本地数据库create database if not exists ck_test002;-- 创建本地表CREATE TABLE ck_test002.example_table( d DateTime, a Int TTL d + INTERVAL 1 MONTH, b Int TTL d + INTERVAL 1 MONTH, c String)ENGINE = MergeTreePARTITION BY toYYYYMM(d)ORDER BY d;show tables from ck_test002;
MergeTree 系列表引擎可以将数据存储在多个块设备上。这对某些可以潜在被划分为“冷”“热”的表来说是很有用的。为了应用存储策略,可以在建表时使用storage_policy设置。
【配置】磁盘、卷和存储策略应当在主配置文件 /etc/clickhouse-server/config.xml 或 /etc/clickhouse-server/config.d 目录中的独立文件中的
示例配置如下:
# 创建存储路径mkdir -p /var/lib/clickhouse/storage001/data{1..3}# 授权chown -R clickhouse:clickhouse /var/lib/clickhouse/storage001
存储配置如下:
/var/lib/clickhouse/storage001/data1/ /var/lib/clickhouse/storage001/data2/ 10485760 /var/lib/clickhouse/storage001/data3/ 10485760
存储策略配置:
disk1 disk2 1073741824 disk3 0.2
标签:
完整配置(/etc/clickhouse-server/config.d/storage001.xml):
/var/lib/clickhouse/storage001/data1/ /var/lib/clickhouse/storage001/data2/ 10485760 /var/lib/clickhouse/storage001/data3/ 10485760 disk1 disk2 1073741824 disk3 0.2
授权
chown -R clickhouse.clickhouse /etc/clickhouse-server/config.d
查看(配置自动加载,不需要重启服务)
# 登录clickhouse-client -u default --password 123456 --port 9000 -h local-168-182-110 --multiquery# 检查SELECT name,path,formatReadableSize(free_space) AS free,formatReadableSize(total_space) AS total,formatReadableSize(keep_free_space) AS reserved from system.disks;SELECT policy_name, volume_name, disks FROM system.storage_policies;
在创建表时,指定存储策略(默认default):
CREATE TABLE table_with_non_default_policy ( EventDate Date, OrderID UInt64, BannerID UInt64, SearchPhrase String)ENGINE = MergeTreeORDER BY (OrderID, BannerID)PARTITION BY toYYYYMM(EventDate)SETTINGS storage_policy = 'moving_from_ssd_to_hdd'
MySQL 引擎可以对存储在远程 MySQL 服务器上的数据执行 SELECT 查询。
【语法】
MySQL('host:port', 'database', 'table', 'user', 'password'[, replace_query, 'on_duplicate_clause']);
参数详解:
【示例】
MySQL操作
mysql -uroot -p -h local-168-182-113密码:123456# 创建数据库create database if not exists ck_test003;USE ck_test003;CREATE TABLE `ck_test003`.`user` ( `id` bigint NOT NULL AUTO_INCREMENT, `account_no` bigint DEFAULT NULL, `phone` varchar(128) COMMENT '手机号', `username` varchar(255) COMMENT '用户名', `create_time` datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;# 添加数据INSERT INTO `ck_test003`.`user`(`id`, `account_no`, `phone`, `username`, `create_time`) VALUES (1, 35, '00719526', 'zhangsan', '2022-07-24 16:02:15');# 查看数据select * from `ck_test003`.`user`;
在clickhouse中创建user表,设置引擎为mysql
# 登录clickhouse-client -h local-168-182-111 -d default -m -u default --password 123456create database if not exists ck_mysql_test003;# 创建表,字段得跟mysql表一一对应CREATE TABLE `ck_mysql_test003`.`user`( id UInt32 , account_no UInt32 , username String , phone String , create_time Datetime) ENGINE = MySQL('local-168-182-113:3306','ck_test003','user','root','123456');# 在clickhouse查询mysql数据select * from `ck_mysql_test003`.`user`;
在clickhouse中添加数据
# 登录clickhouse-client -h local-168-182-111 -d default -m -u default --password 123456# 添加数据INSERT INTO `ck_mysql_test003`.`user`(`id`, `account_no`, `phone`, `username`, `create_time`) VALUES (2, 39, '007996', 'lisi', '2022-07-25 17:02:15');# 查询select * from `ck_mysql_test003`.`user`;
在mysql中查询
mysql -uroot -p -h local-168-182-113密码:123456select * from `ck_test003`.`user`;
首选需要安装HDFS,可以参考我之前的文章:大数据Hadoop原理介绍+安装+实战操作(HDFS+YARN+MapReduce)
环境准备
hostname | ip | 运行角色 |
local-168-182-110 | local-168-182-110 | namenode,datanode ,resourcemanager,nodemanager |
local-168-182-111 | local-168-182-111 | secondarynamedata,datanode,nodemanager |
local-168-182-112 | local-168-182-112 | datanode,nodemanager |
这里不在讲部署的具体过程了,有不清楚的,可以看我上面的文章哦!!!
启动HDFS服务
start-dfs.sh
web访问:http://local-168-182-110:9870
创建ClickHouse存储目录
hdfs dfs -mkdir hdfs://local-168-182-110:8082/clickhouse# 给目录授权hdfs dfs -chown clickhouse:clickhouse hdfs://local-168-182-110:8082/clickhouse
继续说ClickHouse的HDFS引擎
这个引擎提供了与 Apache Hadoop 生态系统的集成,允许通过 ClickHouse 管理 HDFS 上的数据。这个引擎类似于 文件 和 URL 引擎,但提供了 Hadoop 的特定功能。
【语法】
ENGINE = HDFS(URI, format)
【示例】创建 hdfs_engine_table 表
# 登录clickhouse-client -h local-168-182-111 -d default -m -u default --password 123456# 创建表,hdfs_engine_table这个目录是不能提前创建的CREATE TABLE hdfs_engine_table (name String, value UInt32) ENGINE=HDFS('hdfs://local-168-182-110:8082/clickhouse/hdfs_engine_table', 'TSV');# 添加数据INSERT INTO hdfs_engine_table VALUES ('one', 1), ('two', 2), ('three', 3);
在HDFS上查看:http://local-168-182-110:9870
首选需要安装Hive,可以参考我之前的文章:大数据Hadoop之——数据仓库Hive
启动metastore服务
nohup hive --service metastore &ss -atnlp|grep 9083
【语法】ClickHouse使用Hive引擎建表语法如下:
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]( name1 [type1] [ALIAS expr1], name2 [type2] [ALIAS expr2], ...) ENGINE = Hive('thrift://host:port', 'database', 'table');PARTITION BY expr
参数:
使用HDFS文件系统的本地缓存,配置(/etc/clickhouse-server/config.xml)如下:
true local_cache 559096952 1048576
【温馨提示】当ClickHouse为远程文件系统启用了本地缓存时,用户仍然可以选择不使用缓存,并在查询中设置use_local_cache_for_remote_fs = 0 , use_local_cache_for_remote_fs 默认为 false。
在 Hive 中建表:
hive > CREATE TABLE `test`.`test_orc`( `f_tinyint` tinyint, `f_smallint` smallint, `f_int` int, `f_integer` int, `f_bigint` bigint, `f_float` float, `f_double` double, `f_decimal` decimal(10,0), `f_timestamp` timestamp, `f_date` date, `f_string` string, `f_varchar` varchar(100), `f_bool` boolean, `f_binary` binary, `f_array_int` array, `f_array_string` array, `f_array_float` array, `f_array_array_int` array>, `f_array_array_string` array>, `f_array_array_float` array>)PARTITIONED BY ( `day` string)ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat'LOCATION 'hdfs://testcluster/data/hive/test.db/test_orc'OKTime taken: 0.51 secondshive > insert into test.test_orc partition(day='2021-09-18') select 1, 2, 3, 4, 5, 6.11, 7.22, 8.333, current_timestamp(), current_date(), 'hello world', 'hello world', 'hello world', true, 'hello world', array(1, 2, 3), array('hello world', 'hello world'), array(float(1.1), float(1.2)), array(array(1, 2), array(3, 4)), array(array('a', 'b'), array('c', 'd')), array(array(float(1.11), float(2.22)), array(float(3.33), float(4.44)));OKTime taken: 36.025 secondshive > select * from test.test_orc;OK1 2 3 4 5 6.11 7.22 8 2021-11-05 12:38:16.314 2021-11-05 hello world hello world hello world true hello world [1,2,3] ["hello world","hello world"] [1.1,1.2] [[1,2],[3,4]] [["a","b"],["c","d"]] [[1.11,2.22],[3.33,4.44]] 2021-09-18Time taken: 0.295 seconds, Fetched: 1 row(s)
在 ClickHouse 中建表,ClickHouse中的表,从上面创建的Hive表中获取数据:
CREATE TABLE test.test_orc( `f_tinyint` Int8, `f_smallint` Int16, `f_int` Int32, `f_integer` Int32, `f_bigint` Int64, `f_float` Float32, `f_double` Float64, `f_decimal` Float64, `f_timestamp` DateTime, `f_date` Date, `f_string` String, `f_varchar` String, `f_bool` Bool, `f_binary` String, `f_array_int` Array(Int32), `f_array_string` Array(String), `f_array_float` Array(Float32), `f_array_array_int` Array(Array(Int32)), `f_array_array_string` Array(Array(String)), `f_array_array_float` Array(Array(Float32)), `day` String)ENGINE = Hive('thrift://localhost:9083', 'test', 'test_orc')PARTITION BY day;# 查询SELECT * FROM test.test_orc settings input_format_orc_allow_missing_columns = 1\G
首选需要安装KAFKA,可以参考我之前的文章:大数据Hadoop之——Kafka 图形化工具 EFAK(EFAK环境部署)
# 启动服务cd $KAFKA_HOME./bin/kafka-server-start.sh -daemon ./config/server.properties# 创建topic# 创建topic,1副本,1分区,设置数据过期时间72小时(-1表示不过期),单位ms,72*3600*1000=259200000kafka-topics.sh --create --topic clickhouse --bootstrap-server local-168-182-110:9092,local-168-182-111:9092,local-168-182-112:9092 --partitions 1 --replication-factor 1 --config retention.ms=259200000# 查看topic 列表kafka-topics.sh --bootstrap-server local-168-182-110:9092,local-168-182-111:9092,local-168-182-112:9092 --list
继续回到ClickHouse的KAFKA引擎
【语法】
Kafka SETTINGS kafka_broker_list = 'local-168-182-110:9092,local-168-182-111:9092,local-168-182-112:9092', kafka_topic_list = 'clickhouse', kafka_group_name = 'group1', kafka_format = 'JSONEachRow', kafka_row_delimiter = '
', kafka_schema = '', kafka_num_consumers = 2
必要参数:
可选参数:
【示例】
在kafka中添加数据
# 删除topickafka-topics.sh --delete --topic clickhouse --bootstrap-server local-168-182-110:9092,local-168-182-111:9092,local-168-182-112:9092# 新建topickafka-topics.sh --create --topic clickhouse --bootstrap-server local-168-182-110:9092,local-168-182-111:9092,local-168-182-112:9092 --partitions 1 --replication-factor 1 --config retention.ms=259200000# 添加数据(生产者),添加的数据字段要跟CH的字段对应kafka-console-producer.sh --broker-list local-168-182-110:9092 --topic clickhouse{"id":1,"createDate":"2022-08-01","message":"cliclhouse test1"}{"id":2,"createDate":"2022-08-02","message":"cliclhouse test2"}{"id":3,"createDate":"2022-08-03","message":"cliclhouse test3"}# 消费kafka-console-consumer.sh --bootstrap-server local-168-182-110:9092 --topic clickhouse --from-beginning
在ClickHouse中添加表
# 登录,需要加上--stream_like_engine_allow_direct_select 1clickhouse-client -h local-168-182-111 -d default -m -u default --password 123456 --stream_like_engine_allow_direct_select 1CREATE TABLE queue ( id Int16, createDate Date, message String) ENGINE = Kafka('local-168-182-110:9092', 'clickhouse', 'group1', 'JSONEachRow');SELECT * FROM queue LIMIT 3;
成功读取到了数据,并且是从earlast读取,而不是lartest,再执行一次查询,发现数据消失了,这是因为Kafka引擎只能读取消费的数据,读取完了以后就会删除数据,那么用这个引擎就没有意义了,数据只能查询一次,其实Kafka引擎是需要结合物化视图一起使用的,物化视图不断将从kafka接收的消息数据写入到其他表引擎。
CREATE TABLE kafkaMergeTree( id Int16, createDate Date, message String)ENGINE = MergeTreeORDER BY id;CREATE MATERIALIZED VIEW kafkaview TO kafkaMergeTree AS SELECT * FROM queue ;
创建好后,继续向kafka生产数据几条数据,之后查询
select * from kafkaMergeTree;
【示例2】演示一下查询Kafka引擎隐藏列
CREATE TABLE queue2 ( id Int16, createDate Date, message String) ENGINE = Kafka('local-168-182-110:9092', 'clickhouse2', 'group1', 'JSONEachRow');
生产数据
# 新建topickafka-topics.sh --create --topic clickhouse2 --bootstrap-server local-168-182-110:9092,local-168-182-111:9092,local-168-182-112:9092 --partitions 1 --replication-factor 1 --config retention.ms=259200000kafka-console-producer.sh --broker-list local-168-182-110:9092 --topic clickhouse2{"id":1,"createDate":"2022-08-01","message":"cliclhouse test1"}{"id":2,"createDate":"2022-08-02","message":"cliclhouse test2"}{"id":3,"createDate":"2022-08-03","message":"cliclhouse test3"}
查询
select *,_topic,_key,_offset,_timestamp,_partition from queue2;
上方以_开头的是隐藏列,不包含在*之中
在每个节点创建一个数据表,作为一个数据分片,使用ReplicatedMergeTree表引擎实现数据副本,而分布表作为数据写入和查询的入口。这是最常见的集群实现方式。ClickHouse 使用 Apache ZooKeeper 存储副本的元信息。
数据副本官方文档:https://clickhouse.com/docs/zh/engines/table-engines/mergetree-family/replication
-- 创建复制表CREATE TABLE table_name( EventDate DateTime, CounterID UInt32, UserID UInt32) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{layer}-{shard}/table_name', '{replica}')PARTITION BY toYYYYMM(EventDate)ORDER BY (CounterID, EventDate, intHash32(UserID))SAMPLE BY intHash32(UserID)
你可以在服务器的配置文件中指定 Replicated 数据表引擎的默认参数。例如:
/clickhouse/tables/{shard}/{database}/{table} {replica}
这样,你可以在建表时省略参数:
CREATE TABLE table_name ( x UInt32) ENGINE = ReplicatedMergeTreeORDER BY x;
它等价于:
CREATE TABLE table_name ( x UInt32) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/{database}/table_name', '{replica}')ORDER BY x;
【示例】
【温馨提示】{shard}和{replica}会使用macros定义的值,配置文件:/etc/metrika.xml
# node1登录clickhouse-client -h local-168-182-110 -d default -m -u default --password 123456# node2登录clickhouse-client -h local-168-182-110 -d default -m -u default --password 123456-- 在所有节点上创建这个表CREATE TABLE ch_test001( `id` Int16, `createDate` Date, `message` String) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/ch_test001', '{replica}')PARTITION BY toYYYYMM(createDate)ORDER BY id;-- 只在node1节点上添加数据insert into ch_test001 values(1, '2020-01-01', 'test1');-- 在node2上查数据select * from ch_test001;-- 分别登录node3和node4节点-- node3登录clickhouse-client -h local-168-182-112 -d default -m -u default --password 123456-- node4登录clickhouse-client -h local-168-182-113 -d default -m -u default --password 123456-- 在node4节点上添加数据insert into ch_test001 values(1, '2020-01-01', 'test2');-- 在node3上查看副本数据select * from ch_test001;
node1和node2
node3和node4
【温馨提示】删除表再重新建表,发现会失败
Code: 253. DB::Exception: Received from local-168-182-111:9000. DB::Exception: Replica /clickhouse/tables/01/ch_test001/replicas/local-168-182-111-01-2 already exists.
解决:
使用Distributed表引擎创建分布式表:
官方文档:https://clickhouse.com/docs/zh/engines/table-engines/special/distributed/
CREATE TABLE default.ck_distributed on cluster ck_cluster_2022( `id` Int16, `createDate` Date, `message` String)engine = Distributed(ck_cluster_2022,default,ch_test001, rand());# 添加数据insert into ck_distributed values(2, '2020-01-02', 'test2001');# 在哪个节点上添加数据,就是添加到哪个节点的表里。副本会自动同步
【温馨提示】分布式引擎本身不存储数据。
在每个节点创建一个数据表,作为一个数据分片,分布表同时负责分片和副本的数据写入工作。这种实现方案下,不需要使用复制表,但分布表节点需要同时负责分片和副本的数据写入工作,它很有可能称为写入的单点瓶颈。
ZooKeeper不是一个严格的要求:在某些简单的情况下,您可以通过将数据写入应用程序代码中的所有副本来复制数据。 这种方法是不建议的,在这种情况下,ClickHouse将无法保证所有副本上的数据一致性。 因此需要由您的应用来保证这一点。
修改配置即可
false
在每个节点创建一个数据表,作为一个数据分片,同时创建两个分布表,每个分布表(Distributed)节点只纳管一半的数据。副本的实现仍需要借助ReplicatedMergeTree类表引擎。
不同的分片创建不同的表
-- node1登录clickhouse-client -h local-168-182-110 -d default -m -u default --password 123456--node2登录clickhouse-client -h local-168-182-110 -d default -m -u default --password 123456-- 在node1和node2创建这个表CREATE TABLE ch_test002( `id` Int16, `createDate` Date, `message` String) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/ch_test002', '{replica}')PARTITION BY toYYYYMM(createDate)ORDER BY id;# 只在node1节点上添加数据insert into ch_test002 values(1, '2020-01-01', 'test2');# 在node2上查数据select * from ch_test002;# 分别登录node3和node4节点# node3登录clickhouse-client -h local-168-182-112 -d default -m -u default --password 123456# node4登录clickhouse-client -h local-168-182-113 -d default -m -u default --password 123456# 在node3和node4创建这个表CREATE TABLE ch_test003( `id` Int16, `createDate` Date, `message` String) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/ch_test003', '{replica}')PARTITION BY toYYYYMM(createDate)ORDER BY id;# 在node4节点上添加数据insert into ch_test003 values(1, '2020-01-01', 'test3');# 在node3上查看副本数据select * from ch_test003;
创建两个分布表
CREATE TABLE default.ck_distributed_test002 on cluster ck_cluster_2022( `id` Int16, `createDate` Date, `message` String)engine = Distributed(ck_cluster_2022,default,ch_test002, rand());-- 添加数据insert into ck_distributed_test002 values(2, '2020-01-02', 'test2002');CREATE TABLE default.ck_distributed_test003 on cluster ck_cluster_2022( `id` Int16, `createDate` Date, `message` String)engine = Distributed(ck_cluster_2022,default,ch_test003, rand());-- 添加数据insert into ck_distributed_test003 values(2, '2020-01-02', 'test2003');
在每个节点创建两个数据表,同一数据分片的两个副本位于不同节点上,每个分布式表纳管一般的数据。这种方案可以在更少的节点上实现数据分布与冗余,但是部署上略显繁琐。
第一步:修改配置
1 false local-168-182-110 9000 default 123456 local-168-182-111 9000 default 123456 1 false local-168-182-111 9000 default 123456 local-168-182-112 9000 default 123456 1 false local-168-182-112 9000 default 123456 local-168-182-113 9000 default 123456 1 false local-168-182-113 9000 default 123456 local-168-182-110 9000 default 123456 local-168-182-110 2181 local-168-182-111 2181 local-168-182-112 2181 01 local-168-182-110-01-1 ::/0 1073741824 0.01 lz4
上面是node1的配置,其它需要修改配置如下:
02 local-168-182-111-02-1 03 local-168-182-112-03-1 03 local-168-182-113-03-1
接下来就是创建表可以分布表了
-- node1登录clickhouse-client -h local-168-182-110 -d default -m -u default --password 123456-- 集群方式创建,每个节点都会创建一张表,这里需要区分的就是分片和副本分别创建CREATE TABLE ch_shard_test on cluster ( `id` Int16, `createDate` Date, `message` String) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/ch_shard_test', '{replica}')PARTITION BY toYYYYMM(createDate)ORDER BY id;CREATE TABLE ch_replica_test on cluster ( `id` Int16, `createDate` Date, `message` String) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/ch_shard_test', '{replica}')PARTITION BY toYYYYMM(createDate)ORDER BY id;
创建两个分布表
CREATE TABLE default.ck_distributed_shard on cluster ck_cluster_2022( `id` Int16, `createDate` Date, `message` String)engine = Distributed(ck_cluster_2022,default,ch_shard_test, rand());-- 添加数据insert into ck_distributed_shard values(2, '2020-01-02', 'test2002');CREATE TABLE default.ck_distributed_replica on cluster ck_cluster_2022( `id` Int16, `createDate` Date, `message` String)engine = Distributed(ck_cluster_2022,default,ch_replica_test, rand());-- 添加数据insert into ck_distributed_replica values(2, '2020-01-02', 'test2003');
第二步:
【总结】
留言与评论(共有 0 条评论) “” |