目录
1 三种clickhouse 数据库引擎简介
2 clickhous 默认数据库引擎
3 clickhouse mysql 引擎
3.1 clickhouse链接到mysql数据库时可以做的事:
3.2 clickhouse链接到mysql数据库不能做的事:
3.3 使用mysql引擎创建clickhouse数据库
3.4 可以做的事情实操
3.5 不能做的事情验证
4 clickhouse lazy 引擎
1 三种clickhouse 数据库引擎简介
clickhouse 里 所有表都是由数据库引擎所提供的
① clickhous 默认数据库引擎
默认情况下ClickHouse使用自己的数据库引擎,该默认引擎提供可配置的数据库引擎和所有支持的SQL语法.
② clickhouse mysql 引擎
也可以指定使用mysql的引擎,就是可以在clickhouse中调用mysql 中的数据,进行查询,
③ clickhouse lazy 引擎
也可以使用 lazy引擎.
2 clickhous 默认数据库引擎
这里就不细说了,建库的时候不指定数据库引擎的情况下使用的就是默认数据库引擎,何况它和可配置的数据库引擎和所有支持的SQL语法组合使用的内容太多了。不是这里介绍的重点
3 clickhouse mysql 引擎
MySQL引擎用于将远程的MySQL服务器中的表映射到ClickHouse中
3.1 clickhouse链接到mysql数据库时可以做的事:
① 可以执行insert 和select 的操作, 也可以执行show tables; 和show create table_name;
② 也可以进行clickhouse和mysql之间的交互;
3.2 clickhouse链接到mysql数据库不能做的事:
① rename
② create table
③ alter
3.3 使用mysql引擎创建clickhouse数据库
① 语法:
CREATE DATABASE 语法
CREATE DATABASE [IF NOT EXISTS] db_name [ON CLUSTER cluster]
ENGINE = MySQL('host:port', ['database' | database], 'user', 'password')
MySQL数据库引擎参数
host:port — 链接的MySQL地址。
database — 链接的MySQL数据库。
user — 链接的MySQL用户。
password — 链接的MySQL用户密码。
② 建库实例
CREATE DATABASE IF NOT EXISTS db_mysql_ch ENGINE = MySQL('192.168.12.118:3306', 'ezp-bigdata', 'root', '123456')
③ 执行
b64d9704419c :) CREATE DATABASE IF NOT EXISTS db_mysql_ch ENGINE = MySQL('192.168.12.118:3306', 'ezp-bigdata', 'root', '123456')
CREATE DATABASE IF NOT EXISTS db_mysql_ch
ENGINE = MySQL('192.168.12.118:3306', 'ezp-bigdata', 'root', '123456')
Ok.
0 rows in set. Elapsed: 1.410 sec.
b64d9704419c :) show databases;
SHOW DATABASES
┌─name────────┐
│ db_mysql_ch │
│ default │
│ system │
└─────────────┘
3 rows in set. Elapsed: 0.004 sec.
b64d9704419c :)
可见创建完成
3.4 可以做的事情实操
① 进入数据库
b64d9704419c :) use db_mysql_ch;
USE db_mysql_ch
Ok.
0 rows in set. Elapsed: 0.001 sec.
② show tables
b64d9704419c :) show tables;
SHOW TABLES
┌─name────────────────────────────────────────────┐
│ spark_sql_test │
│ spark_sql_test1 │
│ spark_sql_test2 │
│ spark_sql_test3 │
└─────────────────────────────────────────────────┘
40 rows in set. Elapsed: 0.004 sec.
b64d9704419c :)
可见查到的四个表就是对应mysql库ezp-bigdata里原有的表
③ show create table_name
b64d9704419c :) show create spark_sql_test
SHOW CREATE TABLE spark_sql_test
┌─statement──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ CREATE TABLE db_mysql_ch.spark_sql_test (`Id` Int64, `CopId` Nullable(Int32), `BrandId` Nullable(Int32), `VipId` Nullable(Int64), `TotalMoney` Nullable(String), `UpTime` Nullable(Date)) ENGINE = MySQL('192.168.12.118:3306', 'ezp-bigdata', 'spark_sql_test', 'root', '123456') │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
1 rows in set. Elapsed: 0.969 sec.
b64d9704419c :)
④ select 操作
注意clickhouse 是严格区分大消息的所以字段一定要和
b64d9704419c :) select * from spark_sql_test limit 5;
SELECT *
FROM spark_sql_test
LIMIT 5
┌──────Id─┬─CopId─┬─BrandId─┬────VipId─┬─TotalMoney─┬─────UpTime─┐
│ 1225010 │ 1 │ 106 │ 24355184 │ 8058.00 │ 2018-08-27 │
│ 1225011 │ 1 │ 106 │ 24355185 │ 30000.00 │ 2018-09-13 │
│ 1225012 │ 1 │ 106 │ 24355186 │ 0.00 │ 2018-08-23 │
│ 1225013 │ 1 │ 106 │ 24355188 │ 20000.00 │ 2018-09-14 │
│ 1225014 │ 1 │ 106 │ 24355189 │ 90000.00 │ 2018-09-11 │
└─────────┴───────┴─────────┴──────────┴────────────┴────────────┘
5 rows in set. Elapsed: 0.013 sec.
b64d9704419c :) select Id,BrandId from spark_sql_test limit 5;
SELECT
Id,
BrandId
FROM spark_sql_test
LIMIT 5
┌──────Id─┬─BrandId─┐
│ 1225010 │ 106 │
│ 1225011 │ 106 │
│ 1225012 │ 106 │
│ 1225013 │ 106 │
│ 1225014 │ 106 │
└─────────┴─────────┘
5 rows in set. Elapsed: 0.478 sec.
b64d9704419c :)
⑤ insert 操作
b64d9704419c :) INSERT INTO spark_sql_test VALUES (2225018,1,206,2435519,'6000.50','2018-09-17'), (2225019,1,206,2435518,'7000.50', '2018-09-05')
INSERT INTO spark_sql_test VALUES
Ok.
2 rows in set. Elapsed: 0.036 sec.
验证:
spark_sql_test clickhouse 查询
b64d9704419c :) select * from spark_sql_test;
SELECT *
FROM spark_sql_test
┌──────Id─┬─CopId─┬─BrandId─┬────VipId─┬─TotalMoney─┬─────UpTime─┐
│ 1225010 │ 1 │ 106 │ 24355184 │ 8058.00 │ 2018-08-27 │
│ 1225011 │ 1 │ 106 │ 24355185 │ 30000.00 │ 2018-09-13 │
│ 1225012 │ 1 │ 106 │ 24355186 │ 0.00 │ 2018-08-23 │
│ 1225013 │ 1 │ 106 │ 24355188 │ 20000.00 │ 2018-09-14 │
│ 1225014 │ 1 │ 106 │ 24355189 │ 90000.00 │ 2018-09-11 │
│ 1225015 │ 1 │ 106 │ 24355190 │ 8787.00 │ 2018-08-29 │
│ 1225016 │ 1 │ 106 │ 24355191 │ 0.00 │ 2018-08-26 │
│ 1225017 │ 1 │ 106 │ 24355194 │ 14138.00 │ 2018-09-19 │
│ 1225018 │ 1 │ 106 │ 24355195 │ 5000.00 │ 2018-09-17 │
│ 1225019 │ 1 │ 106 │ 24355196 │ 16814.00 │ 2018-09-05 │
│ 2225018 │ 1 │ 206 │ 2435519 │ 6000.50 │ 2018-09-17 │
│ 2225019 │ 1 │ 206 │ 2435518 │ 7000.50 │ 2018-09-05 │
└─────────┴───────┴─────────┴──────────┴────────────┴────────────┘
12 rows in set. Elapsed: 0.009 sec.
spark_sql_test mysql 查询
可见(2225018,1,206,2435519,'6000.50','2018-09-17'), (2225019,1,206,2435518,'7000.50', '2018-09-05')已经插入成功
3.5 不能做的事情验证
① RENAME
b64d9704419c :) rename table spark_sql_test to spark_sql_test_rename
RENAME TABLE spark_sql_test TO spark_sql_test_rename
Received exception from server (version 20.3.21):
Code: 48. DB::Exception: Received from 192.168.12.14:9000. DB::Exception: MySQL: renameTable() is not supported.
0 rows in set. Elapsed: 0.006 sec.
b64d9704419c :)
② CREATE TABLE
b64d9704419c :) CREATE TABLE ch_createtable_test (name String, value UInt32)
Syntax error: failed at position 61 (end of query):
CREATE TABLE ch_createtable_test (name String, value UInt32)
Expected one of: storage definition, ENGINE
这里报错语法错误,因为clickhouse创建表一定要带关键字ENGINE
b64d9704419c :) CREATE TABLE ch_createtable_test (name String, value UInt32) ENGINE = TinyLog
CREATE TABLE ch_createtable_test
(
`name` String,
`value` UInt32
)
ENGINE = TinyLog
Received exception from server (version 20.3.21):
Code: 79. DB::Exception: Received from 192.168.12.14:9000. DB::Exception: Storage TinyLog requires data path.
0 rows in set. Elapsed: 4.359 sec.
b64d9704419c :)
因为clickhouse建表一定要带ENGINE 而mysql表又不支持clickhouse的表引擎所以报错
由上可知不能执行CREATE TABLE
③ ALTER
b64d9704419c :) ALTER TABLE ch_createtable_test ADD addField String
Syntax error: failed at position 37:
ALTER TABLE ch_createtable_test ADD addField String
Expected one of: INDEX, CONSTRAINT, COLUMN
b64d9704419c :)
可见报语法错误
4 clickhouse lazy 引擎
需要继续探索
参考:https://clickhouse.tech/docs/zh/engines/database-engines/