理解与介绍

①clickhouse没有完整的事务支持,无法执行回滚;

②缺少高频率,低延迟的修改或删除已存在数据的能力,仅能用于批量删除或修改数据,但这符合 GDPR;由于删除与修改是异步执行,存在页面删除之后获取数据是为执行完毕状态且无返回值;

③稀疏索引使得ClickHouse不适合通过其键检索单行的点查询;?

④当我们任意分片挂掉的时候,是无法读的Distributed表的,当写入Distributed表是数据分片到挂了的服务时是会报错的;Distributed指的是集群的分布式表,所以线上环境应该部署至少两个副本,来提供稳定性;

⑤Clickhouse支持查询(select)和增加(insert),但是不直接支持更新(update)和删除(delete)。
插入:MergeTree不是LSM树,因为它不包含“memtable”和“log”:插入的数据直接写入文件系统。这使得它仅适用于批量插入数据,而不是非常频繁地插入单行; 每秒一次插入很好,但是每秒一千次不行。如果有大量内容想要插入,可以使用Buffer引擎,Buffer引擎做的是把缓冲数据写入RAM,定期将其刷新到另一个表;

⑥从使用场景来说,Clickhouse是个分析型数据库。这种场景下,数据一般是不变的,因此Clickhouse对update、delete的支持是比较弱的,实际上并不支持标准的update、delete操作;

执行语句

①创建
on cluster perftest_2shards_1replicas中on cluster关键字,perftest_2shards_1replicas为clickHouse集群名称;
CREATE TABLE v_non_motor_vehicle on cluster perftest_2shards_1replicas(non_motor_vehicle_id String , crt_time UInt64) ENGINE MergeTree() primary key non_motor_vehicle_id partition BY toYYYYMMDD(FROM_UNIXTIME(crt_time)) ORDER BY (non_motor_vehicle_id,crt_time) SETTINGS index_granularity=8192;

CREATE TABLE v_face_all AS v_face ENGINE = Distributed(perftest_2shards_1replicas, default, v_face, rand());

②添加
查询与MYSQL大体相似,也可插入集群的分布式表;
insert into v_non_motor_vehicle (face_id,info_kind) VALUES(‘350100123456000000000220201117164935211340621134’,1606810609436);

③删除
突变,删除并且同步语句到每个节点;
ALTER TABLE v_image_info on cluster perftest_2shards_1replicas delete where father_id = ‘350100123456000000000220201117164935211340621134’;

④查询
查询与MYSQL大体相似;
SELECT * FROM v_image_info;

⑤修改
突变,更新并且同步语句到每个节点;
ALTER TABLE v_face on cluster perftest_2shards_1replicas update data_flg = 1 where face_id = ‘350100123456000000000220201117164935211340621134’;

集群部署

①下载镜像,启动容器,拷贝配置文件;

(1)docker pull yandex/clickhouse-server;
(2)docker run -d \
—name clickhouse01 \
-p 9002:9000 \
-p 8125:8123 \
-p 9011:9009 \
—ulimit nofile=262144:262144 \
yandex/clickhouse-server
(3)docker cp clickhouse01:/etc/clickhouse-server/ /home/data/software/v3/middleware

②修改配置文件,容器映射宿主机配置文件路径;

(1)修改users.xml,修改clickhouse用户密码
找到users节点,找到下面的default节点,再找到下面的password节点,这里就是密码,users.xml中默认情况在default下的密码为空;

(2)修改config.xml
1.找到interserver_http_host节点,修改为本机IP 或者 0.0.0.0 ,不要使用127.0.0.1;
2.listen_host修改为本机IP,修改为本机IP 或者 0.0.0.0 ,不要使用127.0.0.1;
3.在该文件的最后的前面一行添加;
/etc/clickhouse-server/metrika.xml
4.在配置文件最下面添加,解决时区问题;
Asia/Shanghai
5.docker映射文件启动命令参考;
docker run -d \
—name clickhouse-server \
—ulimit nofile=262144:262144 \
—volume=/home/data/software/v3/middleware/clickhouse-colony/clickhouse-server1/clickhouse/:/var/lib/clickhouse/ \
—volume=/home/data/software/v3/middleware/clickhouse-colony/clickhouse-server1/clickhouse-server/:/etc/clickhouse-server/ \
—volume=/home/data/software/v3/middleware/clickhouse-colony/clickhouse-server1/log/clickhouse-server/:/var/log/clickhouse-server/ \
-p 9002:9000 \
-p 8125:8123 \
-p 9011:9009 \
yandex/clickhouse-server

③clickhouse集群依赖zookeeper,docker-compose.yml参考;

docker-compose配置clickhouse01、clickhouse02、zookeeper三个容器
version: ‘3.1’
services:
clickhouse01:
image: yandex/clickhouse-server
container_name: clickhouse01
user: root
privileged: true
ports:
- 9000:9000
- 9009:9009
- 8123:8123
volumes:
- “./clickhouse-colony/clickhouse-server1/clickhouse/:/var/lib/clickhouse/“
- “./clickhouse-colony/clickhouse-server1/clickhouse-server/:/etc/clickhouse-server/“
- “./clickhouse-colony/clickhouse-server1/log/clickhouse-server/:/var/log/clickhouse-server/“
ulimits:
nofile:
soft: 262144
hard: 262144
clickhouse02:
image: yandex/clickhouse-server
container_name: clickhouse02
user: root
privileged: true
ports:
- 9001:9000
- 9010:9009
- 8124:8123
volumes:
- “./clickhouse-colony/clickhouse-server2/clickhouse/:/var/lib/clickhouse/“
- “./clickhouse-colony/clickhouse-server2/clickhouse-server/:/etc/clickhouse-server/“
- “./clickhouse-colony/clickhouse-server2/log/clickhouse-server/:/var/log/clickhouse-server/“
ulimits:
nofile:
soft: 262144
hard: 262144
clickhouse-zookeeper:
image: zookeeper
container_name: clickhouse-zookeeper
restart: always
ports:
- 2182:2181
volumes:
- “./clickhouse-colony/zookeeper/conf/:/apache-zookeeper-3.5.6-bin/conf/“
- “./clickhouse-colony/zookeeper/data/:/data”
- “./clickhouse-colony/zookeeper/datalog/:/datalog”
- “./clickhouse-colony/zookeeper/logs/:/logs”

④集群配置文件metrika.xml,存放在容器目录/etc/clickhouse-server/下;

<?xml version=”1.0” encoding=”utf-8”?>






true


xx0

9000

default

xxx




true



xxx

9001

default

xxx








xxxx

2182



01
01

cluster-01-1



::/0




10000000000
0.01
lz4



数据迁移

从 MySQL 到 ClickHouse 未实践

CREATE TABLE [IF NOT EXISTS] [db.]table_name ENGINE = Mergetree AS SELECT * FROM mysql(‘host:port’, ‘db’, ‘database’, ‘user’, ‘password’)

以下如图,性能对比,网图参考
ClickHouse实战经验 - 图1