一、概述

ClickHouse 是俄罗斯的Yandex于2016年开源的列式存储数据库(DBMS),使用C++语言编写,主要用于在线分析处理查询(OLAP),能够使用SQL查询实时生成分析数据报告。

优点

  1. 列式存储(适合聚合操作)
  2. 支持SQL语法
  3. 高吞吐写入(LSM Tree、顺序写)
  4. 多种引擎
  5. 数据分区和并行处理(不利于并发查询)
  6. 单表查询快(单表查询优于关联查询)

总结:适合单表查询且qps低的实时聚合查询业务。

二、安装

1、系统要求

ClickHouse可以在任何具有x86_64,AArch64或PowerPC64LE CPU架构的Linux,FreeBSD或Mac OS X上运行。

官方预构建的二进制文件通常针对x86_64进行编译,并利用SSE 4.2指令集,因此,除非另有说明,支持它的CPU使用将成为额外的系统需求。下面是检查当前CPU是否支持SSE 4.2的命令:

1
$ grep -q sse4_2 /proc/cpuinfo && echo "SSE 4.2 supported" || echo "SSE 4.2 not supported"

要在不支持SSE 4.2AArch64PowerPC64LE架构的处理器上运行ClickHouse,您应该通过适当的配置调整从源代码构建ClickHouse

2、环境准备

主机三台:

HostOS
clinkhouse101Centos7
clinkhouse102Centos7
clinkhouse103Centos7
zookeeperCentos7

关闭linux文件数限制

1
2
3
4
5
6
7
8
9
10
$ vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 131072
* hard nproc 131072
$ vim /etc/security/limits.d/20-nproc.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 131072
* hard nproc 131072

安装依赖

1
2
$ yum install -y libtool
$ yum install -y *unixODBC*

取消SELINUX

1
2
$ vim /etc/selinux/config
SELINUX=disabled

3、安装

官方文档

安装包列表:

  • clickhouse-common-static — ClickHouse编译的二进制文件。
  • clickhouse-server — 创建clickhouse-server软连接,并安装默认配置服务
  • clickhouse-client — 创建clickhouse-client客户端工具软连接,并安装客户端配置文件。
  • clickhouse-common-static-dbg — 带有调试信息的ClickHouse二进制文件。

离线安装:

1
2
3
4
5
6
wget https://packages.clickhouse.com/rpm/stable/clickhouse-client-21.11.11.1-2.noarch.rpm
wget https://packages.clickhouse.com/rpm/stable/clickhouse-common-static-21.11.11.1.x86_64.rpm
wget https://packages.clickhouse.com/rpm/stable/clickhouse-common-static-dbg-21.11.11.1-2.x86_64.rpm
wget https://packages.clickhouse.com/rpm/stable/clickhouse-server-221.11.11.1-2.noarch.rpm

yum install -y clickhouse-*

在线安装:

1
2
3
4
5
6
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://packages.clickhouse.com/rpm/clickhouse.repo
sudo yum install -y clickhouse-server clickhouse-client

sudo /etc/init.d/clickhouse-server start
clickhouse-client # or "clickhouse-client --password" if you set up a password.

如果您想使用最新的版本,请用testing替代stable(我们只推荐您用于测试环境)。prestable有时也可用。

注意: 三台机器都要安装

4、配置

1、修改 /etc/clickhouse-server/config.xml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
$ vim /etc/clickhouse-server/config.xml
<http_port>9123</http_port>
<tcp_port>9000</tcp_port>
# 打开注释,放开允许本机以外的机器访问
<listen_host>::</listen_host>
# 数据文件路径配置
<path>/var/lib/clickhouse/</path>
# 日志文件路径哦配置
<log>/var/log/clickhouse-server/clickhouse-server.log</log>
<zookeeper incl="zookeeper-servers" optional="true" />
<macros incl="macros" optional="true" />
# 修改metrika.xml配置路径
<include_from>/etc/clickhouse-server/config.d/metrika.xml</include_from>

2、创建 /etc/clickhouse-server/config.d/metrika.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?xml version="1.0"?>
<yandex>
<remote_servers>
<!--ck集群名称-->
<test_ck_cluster>
<!-- 集群分片1 -->
<shard>
<weight>1</weight>
<internal_replication>true</internal_replication>
<!-- 该分片的副本 -->
<replica>
<host>clinkhouse101</host>
<port>9000</port>
<user>default</user>
<password></password>
<compression>true</compression>
</replica>
<replica>
<host>clinkhouse102</host>
<port>9000</port>
<user>default</user>
<password></password>
<compression>true</compression>
</replica>
</shard>
<!-- 集群分片2 -->
<shard>
<weight>1</weight>
<internal_replication>true</internal_replication>
<!-- 该分片的副本 -->
<replica>
<host>clinkhouse102</host>
<port>9000</port>
<user>default</user>
<password></password>
<compression>true</compression>
</replica>
<replica>
<host>clinkhouse103</host>
<port>9000</port>
<user>default</user>
<password></password>
<compression>true</compression>
</replica>
</shard>
<!-- 集群分片3 -->
<shard>
<weight>1</weight>
<internal_replication>true</internal_replication>
<!-- 该分片的副本 -->
<replica>
<host>clinkhouse101</host>
<port>9000</port>
<user>default</user>
<password></password>
<compression>true</compression>
</replica>
<replica>
<host>clinkhouse103</host>
<port>9000</port>
<user>default</user>
<password></password>
<compression>true</compression>
</replica>
</shard>
</test_ck_cluster>
</remote_servers>

<!-- zookeeper -->
<zookeeper>
<node index="1">
<host>zookeeper</host>
<port>2181</port>
</node>
<node index="2">
<host>example_host</host>
<port>2181</port>
</node>
<node index="3">
<host>example_host</host>
<port>2181</port>
</node>
</zookeeper>


<!-- 注意修改不同机器上的配置 -->
<macros>
<shard>01</shard> <!--指定的是集群分片信息中的配置,不同机器放的分片数不一样-->
<replica>clinkhouse102</replica> <!--配置当前节点的备份同步节点信息-->
<layer>test_ck_cluster</layer> <!--指定我们的集群标志-->
</macros>

</yandex>

注意:这里是学习用,生产环境注意分片和副本的配置

其他配置

3、分片集群配置问题

副本虽然能够提高数据的可用性,降低丢失风险,但是每台服务器实际上必须容纳全量数据,对数据的横向扩容没有解决。

要解决数据水平切分的问题,需要引入分片的概念。通过分片把一份完整的数据进行切分,不同的分片分布到不同的节点上,再通过Distributed表引擎把数据拼接起来一同使用。

Distributed表引擎本身不存储数据,有点类似于MyCat之于MySql,成为一种中间件,通过分布式逻辑表来写入、分发、路由来操作多台节点不同分片的分布式数据。

注意:ClickHouse的集群是表级别的,实际企业中,大部分做了高可用,但是没有用分片,避免降低查询性能以及操作集群的复杂性。

4、优化

集群分片规划

官方使用建议

三、操作

1、启动

1
$ systemctl start clickhouse-server

2、连接

1
$ clickhouse-client -m

-m :可以在命令窗口输入多行命令

3、查询

1
$ clickhouse-client -h clinkhouse101 --port 9000 --multiquery --query="select * from test"

优化查询性能

4、创建ReplicatedMergeTree表

1
2
3
4
5
6
7
8
9
10
CREATE TABLE default.test ON CLUSTER test_ck_cluster
(
`id` Int64 DEFAULT 0 COMMENT 'id',
`name` Nullable(String) COMMENT '名字',
`create_time` Datetime DEFAULT toDateTime(now()) COMMENT '创建时间'
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/replicated/{shard}/test', '{replica}')
PARTITION BY toYYYYMMDD(create_time)
primary key (id)
ORDER BY id

参数:

  • /clickhouse/tables/ 这一部分指定的是在ZK上创建的路径地址,可随意变换只要记得即可
  • {shard} 指的是分片的标志,同一个分片内的所有机器应该保持相同(不然会导致数据不全)。建议使用使用的是集群名+分片名的配置也就是{layer}-{shard},这里的数据就是在macros中配置的属性
  • test 建议使用表名称
  • {replica} 参数建议在macros配置成机器的hostname,因为每台机器的hostname都是不一样的,因此就能确保每个表的识别符都是唯一的了

5、创建 Distribute 分布式表

1
2
3
4
5
6
create table default.test2 on cluster test_ck_cluster
(
`id` Int64 DEFAULT 0 COMMENT 'id',
`name` Nullable(String) COMMENT '名字',
`create_time` Datetime DEFAULT toDateTime(now()) COMMENT '创建时间'
)engine = Distributed(test_ck_cluster,default, test2,rand());

参数:

  • test_ck_cluster 集群名称
  • default 数据库名称
  • test2 表名
  • rand() 分片键(可选)。必须是整型数字,如果没有则是配置中的weight权重。

注意: Distributed虽然也可以插入,但是主要用于查询。