ClickHouse多级磁盘和冷热数据分离实践

特别注意

  • ck可以大小写区分也可以不区分
  • ck 配置文件中的各个卷的是有顺序的。
开启远程访问

vim /etc/clickhouse-server/config.xml

bash 复制代码
<listen_host>0.0.0.0</listen_host>

前言

ClickHouse 的冷热数据分离和ES的类似,可以选择冷数据跑在哪个数据目录上。

总的来说 ClickHouse 冷热存储架构的整体设计思想是:本地 SSD 存储查询热数据,远端Nas存储查询相对不那么频繁的数据,从而节约存储成本,支持更多的数据存储需求。

操作命令

sql 复制代码
-- 查看存储策略
select * from system.storage_policies

-- 查看磁盘
 select * from system.disks

官网文档

bash 复制代码
## ttl值
https://ClickHouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#mergetree-table-ttl

参考文档

bash 复制代码
###  多级磁盘
https://blog.csdn.net/weixin_37692493/article/details/114118400

### 
https://blog.csdn.net/SmartCodeTech/article/details/127513358

### 不错
https://blog.csdn.net/weixin_47388410/article/details/120885690

线上配置

XML 复制代码
    <storage_configuration>
        <disks>
            <disk0>
                <path>/opt/data/clickhouse/</path>
                <keep_free_space_bytes>1024000000</keep_free_space_bytes>
            </disk0>
            <fast_ssd>
                <path>/opt/data/clickhouse_fast/</path>
                <keep_free_space_bytes>1024000000</keep_free_space_bytes>
            </fast_ssd>
        </disks>

        <policies>
            <single>
                <volumes>
                    <single>
                        <disk>disk0</disk>
                    </single>
                </volumes>
            </single>

            <moving_from_ssd_to_hdd>
                <volumes>
                    <hot>
                        <disk>fast_ssd</disk>
                        <max_data_part_size_bytes>1073741824</max_data_part_size_bytes>
                    </hot>
                    <cold>
                        <disk>disk0</disk>
                    </cold>
                </volumes>
                <move_factor>0.2</move_factor>
            </moving_from_ssd_to_hdd>
        </policies>
        
    </storage_configuration>

示例:

XML 复制代码
<!-- 为了便于查找,我们建议在默认的存储路径下方添加存储策略 -->
<path>/data1/ClickHouse/data/</path> 
    <storage_configuration>
            <disks>
                <hot>
                <!-- 这里注意,使用存储策略后,建议任何数据路径之间不要有子集关系 -->
                    <path>/data1/ClickHouse/hot/</path>  
                </hot>
                <cold>
                    <path>/data2/ClickHouse/cold/</path>
                </cold>
            </disks>
            <policies>
                <ttl>
                    <volumes>
                        <hot>
                           <disk>hot</disk>
                        </hot>
                        <cold>
                           <disk>cold</disk>
                        </cold>
                    </volumes>
                </ttl>
            </policies>
    </storage_configuration>

语法格式

  • < path> 为ClickHouse默认的存储路径,找到该标签后在下方添加存储策略标签<storage_configuration>。
  • <storage_configuration>:固定标签,定义存储策略。
  • < dicks> 固定标签,下面会定义磁盘名称,以及磁盘绝对路径。
  • < hot>、< cold>:自定义标签,用来标记该路径,可按照此名称定义便于区分。
  • < policies>:固定标签,定义具体存储策略名称。
  • < ttl>:自定义标签,定义具体存储策略的名称,用于表级TTL,可按照此名称定义便于区分。
  • < volumes>:固定标签,定义卷组。
  • < hot>、< cold>:卷组名称,每个卷组下可以包括一个或多个disk标签,disk标签取值为< disks >标签下定义的磁盘名称。

检查

bash 复制代码
vm-01 :) select * from system.storage_policies;

SELECT *
FROM system.storage_policies

┌─policy_name────────────┬─volume_name─┬─volume_priority─┬─disks────────┬─volume_type─┬─max_data_part_size─┬─move_factor─┐
│ default                │ default     │               1 │ ['default']  │ JBOD        │                  0 │           0 │
│ moving_from_ssd_to_hdd │ hot         │               1 │ ['fast_ssd'] │ JBOD        │         1073741824 │         0.2 │
│ moving_from_ssd_to_hdd │ cold        │               2 │ ['disk0']    │ JBOD        │                  0 │         0.2 │
│ single                 │ single      │               1 │ ['disk0']    │ JBOD        │                  0 │         0.1 │
└────────────────────────┴─────────────┴─────────────────┴──────────────┴─────────────┴────────────────────┴─────────────┘

4 rows in set. Elapsed: 0.008 sec. 

vm-01 :)  select * from system.disks;

SELECT *
FROM system.disks

┌─name─────┬─path───────────────────────┬──free_space─┬─total_space─┬─keep_free_space─┬─type──┐
│ default  │ /var/lib/clickhouse/       │ 10822782976 │ 18238930944 │               0 │ local │
│ disk0    │ /opt/data/clickhouse/      │  9798782976 │ 17214930944 │      1024000000 │ local │
│ fast_ssd │ /opt/data/clickhouse_fast/ │  9798782976 │ 17214930944 │      1024000000 │ local │
└──────────┴────────────────────────────┴─────────────┴─────────────┴─────────────────┴───────┘

3 rows in set. Elapsed: 0.002 sec. 

-- 切换库
vm-01 :) use default;

--  检查
show create table t1;

创建表

sql 复制代码
-- 创建表

> create table t1(`id` Int32,`name` String) engine=MergeTree() order by id settings storage_policy='moving_from_ssd_to_hdd';



--  检查
SELECT name, data_paths, metadata_path, storage_policy from system.tables where name in ('t1','t2','t3')

模拟写入

sql 复制代码
-- 
insert into t1 values(1,'aa'),(2,'bb'),(3,'cc');

-- check
tree /opt/data/clickhouse_fast/
tree /opt/data/clickhouse/

查看表数据和分区存储信息

sql 复制代码
--  查看表数据和分区存储信息,可以看到按照分区将数据写入到了不同的磁盘目录下
SELECT name, data_paths, metadata_path, storage_policy from system.tables where name ='t1'

--  查看分区存储信息
select name, disk_name, path from system.parts where table = 't1';

多磁盘数据迁移

手动模拟分区合并

手动模拟分区合并,分区合并会自动将其他磁盘目录下数据进行合并,并存储在其中某一磁盘下

sql 复制代码
-- 
optimize table t1;

-- 
select name, disk_name, path from system.parts where table = 't1' and active;
多磁盘分区迁移
bash 复制代码
##
ALTER TABLE t1 MOVE PART 'all_2_2_0' TO VOLUME 'cold'

## 
vm-01 :) select name, disk_name, path from system.parts where table = 't1' and active;

SELECT 
    name,
    disk_name,
    path
FROM system.parts
WHERE (table = 't1') AND active

┌─name──────┬─disk_name─┬─path─────────────────────────────────────────────────┐
│ all_1_1_0 │ fast_ssd  │ /opt/data/clickhouse_fast/data/default/t1/all_1_1_0/ │
│ all_2_2_0 │ fast_ssd  │ /opt/data/clickhouse_fast/data/default/t1/all_2_2_0/ │
└───────────┴───────────┴──────────────────────────────────────────────────────┘

2 rows in set. Elapsed: 0.010 sec. 

vm-01 :) select * from system.storage_policies;

SELECT *
FROM system.storage_policies

┌─policy_name────────────┬─volume_name─┬─volume_priority─┬─disks────────┬─volume_type─┬─max_data_part_size─┬─move_factor─┐
│ default                │ default     │               1 │ ['default']  │ JBOD        │                  0 │           0 │
│ moving_from_ssd_to_hdd │ hot         │               1 │ ['fast_ssd'] │ JBOD        │         1073741824 │         0.2 │
│ moving_from_ssd_to_hdd │ cold        │               2 │ ['disk0']    │ JBOD        │                  0 │         0.2 │
│ single                 │ single      │               1 │ ['disk0']    │ JBOD        │                  0 │         0.1 │
└────────────────────────┴─────────────┴─────────────────┴──────────────┴─────────────┴────────────────────┴─────────────┘

4 rows in set. Elapsed: 0.006 sec. 

vm-01 :) ALTER TABLE t1 MOVE PART 'all_2_2_0' TO VOLUME 'cold'

ALTER TABLE t1
    MOVE PART 'all_2_2_0' TO VOLUME 'cold'


Ok.

0 rows in set. Elapsed: 0.005 sec. 

vm-01 :) select name, disk_name, path from system.parts where table = 't1' and active;

SELECT 
    name,
    disk_name,
    path
FROM system.parts
WHERE (table = 't1') AND active

┌─name──────┬─disk_name─┬─path─────────────────────────────────────────────────┐
│ all_1_1_0 │ fast_ssd  │ /opt/data/clickhouse_fast/data/default/t1/all_1_1_0/ │
│ all_2_2_0 │ disk0     │ /opt/data/clickhouse/data/default/t1/all_2_2_0/      │
└───────────┴───────────┴──────────────────────────────────────────────────────┘

2 rows in set. Elapsed: 0.003 sec. 

策略下沉并应用

通过设置表的TTL值将表的历史的数据下沉到 moving_from_ssd_to_hdd 冷标签(历史元的存储盘)中。

新创建的表将冷数据迁移到 moving_from_ssd_to_hdd 标签磁盘中。
sql 复制代码
-- 创建新表并应用某个存储策略
create table t1(`id` Int32,`name` String) engine=MergeTree() order by id settings storage_policy='moving_from_ssd_to_hdd';


-- 修改表的 TTL
ALTER TABLE example_table MODIFY TTL d + INTERVAL 1 DAY ;
修改原有的表应用某个存储策略
sql 复制代码
-- 查看storage_policies
SELECT 
    policy_name,
    volume_name,
    volume_priority,
    disks,
    formatReadableSize(max_data_part_size) AS max_data_part_size,
    move_factor
FROM system.storage_policies

-- 修改原有的表应用相应的存储策略
alter table  test02 modify setting storage_policy='moving_from_ssd_to_hdd';

冷热数据分离

上面说的磁盘多级磁盘的配置,修改表的存储策略,可以应用到不同的磁盘中。如果通过表的TTL值,自动去对历史数据分到网络存储盘。新数据到本地磁盘了?

基于move factor的数据移动策略
sql 复制代码
vm-01 :)  select * from system.storage_policies;

SELECT *
FROM system.storage_policies

┌─policy_name────────────┬─volume_name─┬─volume_priority─┬─disks────────┬─volume_type─┬─max_data_part_size─┬─move_factor─┐
│ default                │ default     │               1 │ ['default']  │ JBOD        │                  0 │           0 │
│ moving_from_ssd_to_hdd │ hot         │               1 │ ['fast_ssd'] │ JBOD        │         1073741824 │         0.2 │
│ moving_from_ssd_to_hdd │ cold        │               2 │ ['disk0']    │ JBOD        │                  0 │         0.2 │
│ single                 │ single      │               1 │ ['disk0']    │ JBOD        │                  0 │         0.1 │
└────────────────────────┴─────────────┴─────────────────┴──────────────┴─────────────┴────────────────────┴─────────────┘

4 rows in set. Elapsed: 0.004 sec. 

vm-01 :) select * from system.disks;

SELECT *
FROM system.disks

┌─name─────┬─path───────────────────────┬──free_space─┬─total_space─┬─keep_free_space─┬─type──┐
│ default  │ /var/lib/clickhouse/       │ 10813464576 │ 18238930944 │               0 │ local │
│ disk0    │ /opt/data/clickhouse/      │  9789464576 │ 17214930944 │      1024000000 │ local │
│ fast_ssd │ /opt/data/clickhouse_fast/ │  9789464576 │ 17214930944 │      1024000000 │ local │
└──────────┴────────────────────────────┴─────────────┴─────────────┴─────────────────┴───────┘

3 rows in set. Elapsed: 0.005 sec. 

在上面的例子中,我们配置了一个存储策略:

  • 包含了2个卷(volume)。数据默认写入到default磁盘。
  • move factor 设置为0.2。当default磁盘空间小于20%时,将数据迁移到磁盘data2。
  • 默认卷max_data_part_size_bytes设置为xx G,大于xx G的part数据,不会写入到默认卷。

**注意:****配置文件中的各个卷的顺序非常重要。当CK有新数据写入的时候,数据会优先写到第一个卷。再依次写道后面的卷。**move_factor 也是从前面的卷移动到后面的卷。

bash 复制代码
## 以上参考文章如下
https://yunche.pro/blog/?id=64
基于TTL的数据移动策略
sql 复制代码
-- 创建时指定 TTL
CREATE TABLE ttl_test_tbl
(
    `f1` String,
    `f2` String,
    `f3` Int64,
    `f4` Float64,
    `date` Date
)
ENGINE = MergeTree()
PARTITION BY date
ORDER BY f1
TTL date + INTERVAL 90 DAY TO DISK 'disk0'
SETTINGS storage_policy = 'moving_from_ssd_to_hdd';


-- 修改表的 TTL
ALTER TABLE example_table
    MODIFY TTL d + INTERVAL 1 DAY ;
检查
sql 复制代码
vm-01 :) show tables;

SHOW TABLES

┌─name─────────┐
│ enum         │
│ student      │
│ tt           │
│ ttl_test_tbl │
└──────────────┘

4 rows in set. Elapsed: 0.005 sec. 

vm-01 :) desc ttl_test_tbl;

DESCRIBE TABLE ttl_test_tbl

┌─name─┬─type────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ f1   │ String  │              │                    │         │                  │                │
│ f2   │ String  │              │                    │         │                  │                │
│ f3   │ Int64   │              │                    │         │                  │                │
│ f4   │ Float64 │              │                    │         │                  │                │
│ date │ Date    │              │                    │         │                  │                │
└──────┴─────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘

5 rows in set. Elapsed: 0.002 sec. 

vm-01 :) show create table ttl_test_tbl;

SHOW CREATE TABLE ttl_test_tbl

┌─statement──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ CREATE TABLE db_devops.ttl_test_tbl
(
    `f1` String,
    `f2` String,
    `f3` Int64,
    `f4` Float64,
    `date` Date
)
ENGINE = MergeTree()
PARTITION BY date
ORDER BY f1
TTL date + toIntervalDay(90) TO DISK 'disk0'
SETTINGS storage_policy = 'moving_from_ssd_to_hdd', index_granularity = 8192 │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

1 rows in set. Elapsed: 0.002 sec. 

参考文档:

bash 复制代码
## 腾讯云
https://cloud.tencent.com/document/product/1299/63662

修改已有的表的存储策略实践

sql 复制代码
vm-01 :) alter table  knight modify setting storage_policy='moving_from_ssd_to_hdd';

ALTER TABLE knight
    MODIFY SETTING storage_policy = 'moving_from_ssd_to_hdd'



Received exception from server (version 20.8.3):
Code: 36. DB::Exception: Received from 127.0.0.1:9000. DB::Exception: New storage policy shall contain volumes of old one. 

0 rows in set. Elapsed: 0.004 sec. 

报错需要修改如下:

报错的意思是 新的存储策略需要包含旧的磁盘卷。

XML 复制代码
            <moving_from_ssd_to_hdd>
                <volumes>
                 <!-- 增加的部分 -->
                  <default>
                      <disk>default</disk>
                  </default>

                  </default>

                    <hot>
                        <disk>fast_ssd</disk>
                        <max_data_part_size_bytes>1073741824</max_data_part_size_bytes>
                    </hot>

                    <cold>
                        <disk>disk0</disk>
                    </cold>
                </volumes>
                <move_factor>0.2</move_factor>
            </moving_from_ssd_to_hdd>
相关推荐
risc1234565 天前
【ClickHouse】RollingBitmap
clickhouse
斯特凡今天也很帅5 天前
clickhouse如何查看操作记录,从日志来查看写入是否成功
数据库·clickhouse
袖清暮雨10 天前
ClickHouse讲解
大数据·数据库·数据仓库·clickhouse·oracle
江枫渔火L11 天前
使用clickhouse的ReplacingMergeTree引擎表做活跃玩家信息表
数据库·clickhouse
潇凝子潇13 天前
Doris ClickHouse Greenplum 对比
clickhouse·doris·greenplum
递归尽头是星辰15 天前
ClickHouse核心优势分析与场景实战
大数据·数据仓库·clickhouse·实时分析·实时查询
鲁尼的小宝贝16 天前
基于Flink的数据中台管理平台
java·大数据·clickhouse·flink·yarn
问道飞鱼20 天前
【大数据知识】今天聊聊Clickhouse部署方案
大数据·clickhouse·部署
Fireworkitte23 天前
ClickHouse详解
clickhouse
知其_所以然24 天前
使用docker安装clickhouse集群
clickhouse·docker·容器