特別注意
- ck可以大小寫區(qū)分也可以不區(qū)分
- ck?配置文件中的各個卷的是有順序的。
開啟遠(yuǎn)程訪問
?vim /etc/clickhouse-server/config.xml
<listen_host>0.0.0.0</listen_host>
前言
ClickHouse 的冷熱數(shù)據(jù)分離和ES的類似,可以選擇冷數(shù)據(jù)跑在哪個數(shù)據(jù)目錄上。
總的來說 ClickHouse 冷熱存儲架構(gòu)的整體設(shè)計思想是:本地 SSD 存儲查詢熱數(shù)據(jù),遠(yuǎn)端Nas存儲查詢相對不那么頻繁的數(shù)據(jù),從而節(jié)約存儲成本,支持更多的數(shù)據(jù)存儲需求。
操作命令
-- 查看存儲策略
select * from system.storage_policies
-- 查看磁盤
select * from system.disks
官網(wǎng)文檔
## ttl值
https://ClickHouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#mergetree-table-ttl
參考文檔
### 多級磁盤
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
線上配置
<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>
示例:
<!-- 為了便于查找,我們建議在默認(rèn)的存儲路徑下方添加存儲策略 -->
<path>/data1/ClickHouse/data/</path>
<storage_configuration>
<disks>
<hot>
<!-- 這里注意,使用存儲策略后,建議任何數(shù)據(jù)路徑之間不要有子集關(guān)系 -->
<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默認(rèn)的存儲路徑,找到該標(biāo)簽后在下方添加存儲策略標(biāo)簽<storage_configuration>。
- <storage_configuration>:固定標(biāo)簽,定義存儲策略。
- < dicks> 固定標(biāo)簽,下面會定義磁盤名稱,以及磁盤絕對路徑。
- < hot>、< cold>:自定義標(biāo)簽,用來標(biāo)記該路徑,可按照此名稱定義便于區(qū)分。
- < policies>:固定標(biāo)簽,定義具體存儲策略名稱。
- < ttl>:自定義標(biāo)簽,定義具體存儲策略的名稱,用于表級TTL,可按照此名稱定義便于區(qū)分。
- < volumes>:固定標(biāo)簽,定義卷組。
- < hot>、< cold>:卷組名稱,每個卷組下可以包括一個或多個disk標(biāo)簽,disk標(biāo)簽取值為< disks >標(biāo)簽下定義的磁盤名稱。
檢查
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;
創(chuàng)建表
-- 創(chuàng)建表
> 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')
模擬寫入
--
insert into t1 values(1,'aa'),(2,'bb'),(3,'cc');
-- check
tree /opt/data/clickhouse_fast/
tree /opt/data/clickhouse/
查看表數(shù)據(jù)和分區(qū)存儲信息
-- 查看表數(shù)據(jù)和分區(qū)存儲信息,可以看到按照分區(qū)將數(shù)據(jù)寫入到了不同的磁盤目錄下
SELECT name, data_paths, metadata_path, storage_policy from system.tables where name ='t1'
-- 查看分區(qū)存儲信息
select name, disk_name, path from system.parts where table = 't1';
多磁盤數(shù)據(jù)遷移
手動模擬分區(qū)合并
手動模擬分區(qū)合并,分區(qū)合并會自動將其他磁盤目錄下數(shù)據(jù)進行合并,并存儲在其中某一磁盤下
--
optimize table t1;
--
select name, disk_name, path from system.parts where table = 't1' and active;
多磁盤分區(qū)遷移
##
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.
策略下沉并應(yīng)用
通過設(shè)置表的TTL值將表的歷史的數(shù)據(jù)下沉到 moving_from_ssd_to_hdd 冷標(biāo)簽(歷史元的存儲盤)中。
新創(chuàng)建的表將冷數(shù)據(jù)遷移到 moving_from_ssd_to_hdd?標(biāo)簽磁盤中。
-- 創(chuàng)建新表并應(yīng)用某個存儲策略
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 ;
修改原有的表應(yīng)用某個存儲策略
-- 查看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
-- 修改原有的表應(yīng)用相應(yīng)的存儲策略
alter table test02 modify setting storage_policy='moving_from_ssd_to_hdd';
冷熱數(shù)據(jù)分離
上面說的磁盤多級磁盤的配置,修改表的存儲策略,可以應(yīng)用到不同的磁盤中。如果通過表的TTL值,自動去對歷史數(shù)據(jù)分到網(wǎng)絡(luò)存儲盤。新數(shù)據(jù)到本地磁盤了?
基于move factor的數(shù)據(jù)移動策略
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)。數(shù)據(jù)默認(rèn)寫入到default磁盤。
- move factor?設(shè)置為0.2。當(dāng)default磁盤空間小于20%時,將數(shù)據(jù)遷移到磁盤data2。
- 默認(rèn)卷max_data_part_size_bytes設(shè)置為xx G,大于xx G的part數(shù)據(jù),不會寫入到默認(rèn)卷。
注意: 配置文件中的各個卷的順序非常重要。當(dāng)CK有新數(shù)據(jù)寫入的時候,數(shù)據(jù)會優(yōu)先寫到第一個卷。再依次寫道后面的卷。move_factor 也是從前面的卷移動到后面的卷。
## 以上參考文章如下
https://yunche.pro/blog/?id=64
基于TTL的數(shù)據(jù)移動策略
-- 創(chuàng)建時指定 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 ;
檢查
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.
參考文檔:
## 騰訊云
https://cloud.tencent.com/document/product/1299/63662
修改已有的表的存儲策略實踐
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.
報錯需要修改如下:文章來源:http://www.zghlxwxcb.cn/news/detail-695776.html
報錯的意思是 新的存儲策略需要包含舊的磁盤卷。文章來源地址http://www.zghlxwxcb.cn/news/detail-695776.html
<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>
到了這里,關(guān)于ClickHouse多級磁盤和冷熱數(shù)據(jù)分離實踐的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!