0
点赞
收藏
分享

微信扫一扫

click使用CRUD

ixiaoyang8 2022-08-20 阅读 67

0. 基于docker 启动镜像

docker run -d --name clickhouse-server1 --ulimit nofile=262144:262144 clickhouse/clickhouse-server

1. 简单使用

  1. 创建数据库

ac9033c7171a :) create database if not exists helloworld;

CREATE DATABASE IF NOT EXISTS helloworld

Query id: 91b9d3a9-7c42-447b-bd7a-264db60fbf47

Ok.

0 rows in set. Elapsed: 0.004 sec.

ac9033c7171a :) show databases;

SHOW DATABASES

Query id: 4ad374bc-579b-4493-802c-f55380c6fa04

┌─name───────────────┐
│ INFORMATION_SCHEMA │
│ default │
│ helloworld │
│ information_schema │
│ system │
└────────────────────┘

5 rows in set. Elapsed: 0.002 sec.

  1. 建立表

不指定数据库的时候默认是插入到default 数据库。

CREATE TABLE helloworld.my_first_table
(
user_id UInt32,
message String,
timestamp DateTime,
metric Float32
)
ENGINE = MergeTree()
PRIMARY KEY (user_id, timestamp)

  1. 插入数据

INSERT INTO helloworld.my_first_table (user_id, message, timestamp, metric) VALUES
(101, 'Hello, ClickHouse!', now(), -1.0 ),
(102, 'Insert a lot of rows per batch', yesterday(), 1.41421 ),
(102, 'Sort your data based on your commonly-used queries', today(), 2.718 ),
(101, 'Granules are the smallest chunks of data read', now() + 5, 3.14159 )

  1. 查询

ac9033c7171a :) use helloworld;

USE helloworld

Query id: 272a78ba-e8df-4422-9f7a-41898fd300a8

Ok.

0 rows in set. Elapsed: 0.001 sec.

ac9033c7171a :) select * from my_first_table;

SELECT *
FROM my_first_table

Query id: 004aa072-7e85-4533-92b4-cfaf6598d970

┌─user_id─┬─message────────────────────────────────────────────┬───────────timestamp─┬──metric─┐
101 │ Hello, ClickHouse!2022-08-09 04:44:06-1
101 │ Granules are the smallest chunks of data read │ 2022-08-09 04:44:113.14159
102Insert a lot of rows per batch │ 2022-08-08 00:00:001.41421
102 │ Sort your data based on your commonly-used queries │ 2022-08-09 00:00:002.718
└─────────┴────────────────────────────────────────────────────┴─────────────────────┴─────────┘

4 rows in set. Elapsed: 0.005 sec.

  1. 插入csv文件的数据

echo '''102,This is data in a file,2022-02-22 10:43:28,123.45
> 101,It is comma-separated,2022-02-23 00:00:00,456.78
> 103,Use FORMAT to specify the format,2022-02-21 10:43:30,678.90''' > data.csv

clickhouse-client --query='INSERT INTO helloworld.my_first_table FORMAT CSV' < data.csv

root@ac9033c7171a:/# clickhouse-client --query='select * from helloworld.my_first_table'
101 Hello, ClickHouse! 2022-08-09 04:44:06 -1
101 Granules are the smallest chunks of data read 2022-08-09 04:44:11 3.14159
102 Insert a lot of rows per batch 2022-08-08 00:00:00 1.41421
102 Sort your data based on your commonly-used queries 2022-08-09 00:00:00 2.718
101 It is comma-separated 2022-02-23 00:00:00 456.78
102 This is data in a file 2022-02-22 10:43:28 123.45
103 Use FORMAT to specify the format 2022-02-21 10:43:30 678.9

2. sql 操作

基本来说传统关系数据库(以MySQL 为例)的SQL语句,ClickHouse 基本都支持。下面简单介绍其用法。

1. 创建数据库和表

--- 创建数据库
create database if not exists test;

--- 创建表
use test;

--- user 表
CREATE TABLE t_user
(
id UInt32,
username String,
fullname String,
birth_day DateTime,
age UInt32
)
ENGINE = MergeTree()
PRIMARY KEY (id)

--- t_user_log 表
CREATE TABLE t_user_log
(
id UInt32,
t_user_id UInt32,
msg String,
createtime DateTime
)
ENGINE = MergeTree()
PRIMARY KEY (id)

查看建表语句:

ac9033c7171a :) show create table t_user;

SHOW CREATE TABLE t_user

Query id: b962b3e3-47a7-42a1-bd4b-2189389924e0

┌─statement───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
CREATE TABLE test.t_user
(
`id` UInt32,
`username` String,
`fullname` String,
`birth_day` DateTime,
`age` UInt32
)
ENGINE = MergeTree
PRIMARY KEY id
ORDER BY id
SETTINGS index_granularity = 8192
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

1 rows in set. Elapsed: 0.002 sec.

2. 插入

语法:

insert into [table_name] values(...), (...)
或者
insert into [table_name] select a, b, c from [table2_name]

插入测试数据:

--- 插入t_user 表
insert into t_user values
(1,'zs','张三','2020-06-01 12:00:00',23),
(2,'ls','李四','2020-06-02 12:00:00',24),
(3,'ww','王五','2020-06-03 12:00:00',25),
(4,'tq','田七','2020-06-04 12:00:00',26),
(5,'wb','王八','2020-06-05 12:00:00',27),
(6,'gj','鬼酒','2020-06-06 12:00:00',28),
(7,'zs1','张三','2020-06-01 12:00:00',23)

--- t_user_log 表
insert into t_user_log values
(1,1,'张三登录1','2020-06-01 12:00:00'),
(2,1,'张三登录2','2020-06-01 13:00:00'),
(3,1,'张三登录3','2020-06-01 14:00:00'),
(4,1,'张三登录4','2020-06-01 15:00:00'),
(5,2,'测试1','2020-06-01 12:00:00'),
(6,2,'测试2','2020-06-01 13:00:00'),
(7,2,'测试3','2020-06-01 14:00:00'),
(8,2,'测试4','2020-06-01 15:00:00'),
(9,3,'测试4','2020-06-01 15:00:00')

3. update 和 delete

ck的update和delete同mysql的不一样,ck的操作被称为mutation(突变)查询。并且是通过alter方式实现更新、删除。语法为:

alter table t_user_log delete where id = 9;
alter table t_user_log update msg='修改后的测试8',t_user_id=1 where id = 8

mutation与标准的update、delete主要区别是:标准SQL的更新、删除操作是同步的,即客户端要等服务端反回执行结果(通常是int值);而ck的update、delete是通过异步方式实现的,当执行update语句时,服务端立即反回,但是实际上此时数据还没变,而是排队等着, 另外mutation是一种很重(每次修改、删除都会导致放弃目标数据的原有分区而重新分区)的操作\不支持事务。因此对于ck尽量做批量的变更,不进行频繁小数据的操作。

由于操作比较重,所以mutation语句分两步执行,同步执行的部分其实只是进行新增数据新增分区并把旧分区打上逻辑上的失效标记。直到触发分区合并的时候,才会删除旧数据释放磁盘空间。

1》 查看mutation 队列

SELECT
database,
table,
command,
create_time,
is_done
FROM system.mutations
LIMIT 10

2》删除队列中的任务

如果发现某个任务造成卡顿,也可以删掉某个任务。下面的条件是system.mutations 表中的数据。

kill mutation where database='test' and table='t_user_log';

4. 查询操作

ck基本上与标准sql查询差别不大,支持:子查询、支持CTE(CommonTableExpression 公用表达式with字句)、支持各种join(join无法使用缓存)、窗口函数、group by 操作增加了with rollup、with cube、with total 用来计算小计和总计。

1》联查

c0bf6ba896ed :) select u.username, u.fullname, u.birth_day, l.msg from t_user as u inner join t_user_log as l  on u.id = l.t_user_id

SELECT
u.username,
u.fullname,
u.birth_day,
l.msg
FROM t_user AS u
INNER JOIN t_user_log AS l ON u.id = l.t_user_id

Query id: 246c6088-f079-4a7a-9bb0-bec4d0735e94

┌─username─┬─fullname─┬───────────birth_day─┬─msg───────────┐
│ zs │ 张三 │ 2020-06-01 12:00:00 │ 张三登录1 │
│ zs │ 张三 │ 2020-06-01 12:00:00 │ 张三登录2 │
│ zs │ 张三 │ 2020-06-01 12:00:00 │ 张三登录3 │
│ zs │ 张三 │ 2020-06-01 12:00:00 │ 张三登录4 │
│ zs │ 张三 │ 2020-06-01 12:00:00 │ 修改后的测试8 │
│ ls │ 李四 │ 2020-06-02 12:00:00 │ 测试1 │
│ ls │ 李四 │ 2020-06-02 12:00:00 │ 测试2 │
│ ls │ 李四 │ 2020-06-02 12:00:00 │ 测试3 │
└──────────┴──────────┴─────────────────────┴───────────────┘

2》with roll up: 从右至左去掉维护进行小计

c0bf6ba896ed :) select fullname, id, sum(age) from t_user group by fullname, id  with rollup

SELECT
fullname,
id,
sum(age)
FROM t_user
GROUP BY
fullname,
id
WITH ROLLUP

Query id: 8f3c630c-4254-4cb4-b36b-fb7dc8858b69

┌─fullname─┬─id─┬─sum(age)─┐
│ 田七 │ 426
│ 王八 │ 527
│ 王五 │ 325
│ 张三 │ 723
│ 张三 │ 123
│ 鬼酒 │ 628
│ 李四 │ 224
└──────────┴────┴──────────┘
┌─fullname─┬─id─┬─sum(age)─┐
│ 王五 │ 025
│ 张三 │ 046
│ 田七 │ 026
│ 鬼酒 │ 028
│ 王八 │ 027
│ 李四 │ 024
└──────────┴────┴──────────┘
┌─fullname─┬─id─┬─sum(age)─┐
│ │ 0176
└──────────┴────┴──────────┘

14 rows in set. Elapsed: 0.004 sec.

3》with cube 从右至左去掉维度进行小计,再从左至右去掉维度进行小计

c0bf6ba896ed :) select fullname, id, sum(age) from t_user group by fullname, id  with rollup

SELECT
fullname,
id,
sum(age)
FROM t_user
GROUP BY
fullname,
id
WITH ROLLUP

Query id: 8f3c630c-4254-4cb4-b36b-fb7dc8858b69

┌─fullname─┬─id─┬─sum(age)─┐
│ 田七 │ 426
│ 王八 │ 527
│ 王五 │ 325
│ 张三 │ 723
│ 张三 │ 123
│ 鬼酒 │ 628
│ 李四 │ 224
└──────────┴────┴──────────┘
┌─fullname─┬─id─┬─sum(age)─┐
│ 王五 │ 025
│ 张三 │ 046
│ 田七 │ 026
│ 鬼酒 │ 028
│ 王八 │ 027
│ 李四 │ 024
└──────────┴────┴──────────┘
┌─fullname─┬─id─┬─sum(age)─┐
│ │ 0176
└──────────┴────┴──────────┘

14 rows in set. Elapsed: 0.004 sec.

4》with totals: 只计算合计

c0bf6ba896ed :) select fullname, id, sum(age) from t_user group by fullname, id  with totals

SELECT
fullname,
id,
sum(age)
FROM t_user
GROUP BY
fullname,
id
WITH TOTALS

Query id: 7d75b63b-8845-4461-8419-4f2ba130b0d8

┌─fullname─┬─id─┬─sum(age)─┐
│ 田七 │ 426
│ 王八 │ 527
│ 王五 │ 325
│ 张三 │ 723
│ 张三 │ 123
│ 鬼酒 │ 628
│ 李四 │ 224
└──────────┴────┴──────────┘

Totals:
┌─fullname─┬─id─┬─sum(age)─┐
│ │ 0176
└──────────┴────┴──────────┘

7 rows in set. Elapsed: 0.004 sec.

5. alter 操作

和mysql 的修改字段基本一致

1》新增字段:

alter table tableName add column newcolname String after col1

2》修改字段

alter table tableName modify column newcolname String

3》删除字段

a lter table tableName drop column colname

6. 导出数据

参考: ​​https://clickhouse.com/docs/en/interfaces/formats/​​

【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】



举报

相关推荐

0 条评论