ClickHouse可运行于任何x86 64位的Linux, FreeBSD, 或 Mac OS X ,及 AArch64, 或PowerPC64LE CPU架构。下列步骤将在Linux上安装和运行ClickHouse。
1. 启动Clickhouse
1)下载Clickhouse到本地的最简单方法是运行如下命令。如果操作系统支持,将会下载一个相应的Clickhouse二进制文件且可执行:
curl https://clickhouse.com/ | sh
2)运行install命令,其定义Clickhouse使用的文件、文件夹和符号链接的一个集合——这些在安装脚本的输出中都可以看到:
sudo ./clickhouse install
安装脚本最后,会提示输入default用户的口令。输入任何口令,或者选择空着:
Creating log directory /var/log/clickhouse-server.
Creating data directory /var/lib/clickhouse.
Creating pid directory /var/run/clickhouse-server.
chown -R clickhouse:clickhouse '/var/log/clickhouse-server'
chown -R clickhouse:clickhouse '/var/run/clickhouse-server'
chown clickhouse:clickhouse '/var/lib/clickhouse'
Enter password for default user:
将会看到下列输出:
ClickHouse has been successfully installed.
Start clickhouse-server with:
sudo clickhouse start
Start clickhouse-client with:
clickhouse-client
3)运行下列命令以启动Clickhouse服务器:
sudo clickhouse start
2. 连接到ClickHouse
可以使用内建UI或Clickhouse客户端连接到Clickhouse。
2.1 用内建UI
1)Clickhouse服务器默认在8123端口监听HTTP客户端。http://127.0.0.1:8123/play有一个内建UI用于运行SQL查询(需相应改变主机名)。
2)使用Play UI时注意,用户名填写default,密码空着。如果之前为default用户设置了口令,则键入该口令即可。
3)试着运行查询。例如:下面返回预定义数据库的名称:
SHOW databases
4)单击“RUN”按钮,将在Play UI下半部分显示回应信息:
2.2 使用Clickhouse客户端
可以用叫做clickhouse-client的命令行工具连到Clickhouse服务器:
clickhouse-client
如果出现笑脸提示符“:)”,说明可以运行查询。
1)试着运行如下查询:
SHOW TABLES FROM system LIMIT 10
┌─name───────────────────────────┐
│ aggregate_function_combinators │
│ asynchronous_inserts │
│ asynchronous_metric_log │
│ asynchronous_metrics │
│ backups │
│ build_options │
│ certificates │
│ clusters │
│ collations │
│ columns │
└────────────────────────────────┘
10 rows in set. Elapsed: 0.003 sec.
3. 创建数据库和表
3.1 创建数据库
像多数管理系统一样,Clickhouse逻辑上将表分组成数据库。用CREATE DATABASE命令在Clickhouse中创建一个数据库:
CREATE DATABASE IF NOT EXISTS helloworld
3.2 创建表
运行如下命令在helloworld数据库中创建my_first_table表:
CREATE TABLE helloworld.my_first_table
(
user_id UInt32,
message String,
timestamp DateTime,
metric Float32
)
ENGINE = MergeTree()
PRIMARY KEY (user_id, timestamp)
上述例子中,my_first_table为包含4个字段列的MergeTree表:
1)user_id:32位无符号整数;
2)message:字符串类型,其替代其他数据库系统中类似VARCHAR, BLOB, CLOB 和其他类型;
3)timestamp:日期时间值,其表示一个时刻;
4)metric:32位的浮点数;
--注:
1)表引擎:表引擎确定数据如何存储以及存储在哪里、支持哪些查询以及是否复制数据。有许多引擎可供选择,但对于单节点ClickHouse服务器上的简单表,MergeTree可能是您的选择。
3.3 主键简介
进一步学习前,理解主键在Clickhouse中如何工作很重要(主键实现也许出乎意料):
1)Clickhouse的主键对表中的每行数据并非唯一。
Clickhouse表的主键决定数据写到磁盘时如何排序。每8192行或10MB数据(被参考作索引粒度)在主键索引文件中创建一个条目。该粒度概念创建一个能容易存储于内存中的稀疏索引,且该颗粒表示select查询期间处理的一条最少数量的列数据(就是查询操作数据的最小粒度)。
主键可以用PRIMARY KEY命令进行定义。如果定义表时没指定PRIMARY KEY,那么,ORDER BY子句指定的组合将变为主键。如果同时指定了PRIMARY KEY和ORDER BY,主键则必须为排序字段的子集。
主键也是排序键,其为(user_id,timestamp)的组合。所以,存储于每个列文件中的数据将先通过user_id,再按照timestamp,进行排序。
3.4 插入(Insert)数据
Clickhouse中可以使用大家熟悉的INSERT INTO TABLE命令,但重要的是要理解,对MergeTree表的每个插入操作都会导致在存储中创建一个部分(part)。
Clickhouse的最佳实践是每个批次插入大量数据行——每次数万甚至数百万行数据。(别急——Clickhouse能轻松处理这个量级的数据!)
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)注意timestamp列通过各种date和datetime函数填充。Clickhouse中有数百函数可用。
2)让我们看看其是否正常工作:
SELECT * FROM helloworld.my_first_table
应该看到插入了4行数据:
注意到回应信息很好的进行了格式化:
SELECT *
FROM helloworld.my_first_table
ORDER BY timestamp ASC
Query id: f7a33012-bc8c-4f0f-9641-260ee1ffe4b8
┌─user_id─┬─message────────────────────────────────────────────┬───────────timestamp─┬──metric─┐
│ 102 │ Insert a lot of rows per batch │ 2022-03-21 00:00:00 │ 1.41421 │
│ 102 │ Sort your data based on your commonly-used queries │ 2022-03-22 00:00:00 │ 2.718 │
│ 101 │ Hello, ClickHouse! │ 2022-03-22 14:04:09 │ -1 │
│ 101 │ Granules are the smallest chunks of data read │ 2022-03-22 14:04:14 │ 3.14159 │
└─────────┴────────────────────────────────────────────────────┴─────────────────────┴─────────┘
4 rows in set. Elapsed: 0.008 sec.
3)增加一个FORMAT子句用以指定Clickhouse支持的多个输出格式之一:
SELECT *
FROM helloworld.my_first_table
ORDER BY timestamp ASC
FORMAT Vertical
上述查询返回垂直列表输出。这对包含长串的数据很有用,否则,在终端或Play UI中很难阅读。也可以指定CSV,TSV,或多种其他格式。
Row 1:
──────
user_id: 102
message: Insert a lot of rows per batch
timestamp: 2022-08-18 00:00:00
metric: 1.41421
Row 2:
──────
user_id: 102
message: Sort your data based on your commonly-used queries
timestamp: 2022-08-19 00:00:00
metric: 2.71
Row 3:
──────
user_id: 101
message: Hello, ClickHouse!
timestamp: 2022-08-19 13:44:07
metric: -1
Row 4:
──────
user_id: 101
message: Granules are the smallest chunks of data read
timestamp: 2022-08-19 13:44:12
metric: 3.14159
3.5 插入 CSV文件
开始用一种数据库时,最常见的任务是将文件中已有数据插入库中。这里有一些表示点击流的可插入的在线样本数据——其包含一个用户ID,一个访问的URL和事件时间。
如果一直使用Play UI(web接口),这一步请切换到命令行,因为将通过磁盘文件工作。
1)假设我们有如下名字为data.csv的CSV文件中的文本:
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
2)如下命令将上述文件中的数据插入my_first_table中:
clickhouse-client \
--query='INSERT INTO helloworld.my_first_table FORMAT CSV' < data.csv
3)运行如下查询来核验之前的插入操作:
SELECT *
FROM helloworld.my_first_table
ORDER BY timestamp ASC
SELECT *
FROM helloworld.my_first_table
ORDER BY timestamp ASC
Query id: d7216864-2b85-4ad2-9073-6c0bef7ed0c6
┌─user_id─┬─message────────────────────────────────────────────┬───────────timestamp─┬──metric─┐
│ 103 │ Use FORMAT to specify the format │ 2022-02-21 10:43:30 │ 678.9 │
│ 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 │
│ 102 │ Insert a lot of rows per batch │ 2022-07-15 00:00:00 │ 1.41421 │
│ 102 │ Sort your data based on your commonly-used queries │ 2022-07-16 00:00:00 │ 2.718 │
│ 101 │ Hello, ClickHouse! │ 2022-07-16 00:19:30 │ -1 │
│ 101 │ Granules are the smallest chunks of data read │ 2022-07-16 00:19:35 │ 3.14159 │
└─────────┴────────────────────────────────────────────────────┴─────────────────────┴─────────┘
--提示:
1)如果输出结果中看到两套数据行,意味着数据存储到磁盘的多个部分(parts)中。这是期待的,因为前面运行了两个insert查询,一个在客户端,一个在CSV文件中。