6W+字記錄實驗全過程 | 探索Alluxio經濟化資料儲存策略

語言: CN / TW / HK

探索背景

隨著大資料應用的不斷髮展,資料倉庫、資料湖的大資料實踐層出不窮;無論是電信、金融、政府,各個行業的大資料熱潮蓬勃發展。在過去的4-5年中,我們不斷看到企業使用者大資料膨脹問題日益加劇,大資料創新下資料儲存成本呈現線性增長,使得企業對於大資料的應用開始變得謹慎、變向放緩了企業內部資料化轉型的速度。

核心的挑戰:如何更加經濟地構建資料湖儲存體系。

大資料儲存引擎從2006年釋出以來,百花齊放,計算側MapReduce、Spark、Hive、Impala、Presto、Storm、Flink的問世不斷突破應用領域,不過在大資料儲存方面反而顯得慎重與沉穩。在過去十多年,在Apache Hadoop生態被廣泛提及的主要還是HDFS與Ozone。

HDFS

Hadoop HDFS 是一種分散式檔案系統,旨在在商用硬體上執行以提高其普適性。它與現有的分散式檔案系統有很多相似之處。但是,HDFS的特點也是鮮明的:具備高度容錯性、旨在部署在低成本硬體、允許水平擴縮容。HDFS提供對應用程式資料訪問的高吞吐量,適用於需要處理海量資料集的應用服務。

Ozone

Apache Ozone 是一種高度可擴充套件的分散式儲存,適用於分析、大資料和雲原生應用程式。Ozone 支援 S3 相容物件 API 以及 Hadoop 相容檔案系統協議。它針對高效的物件儲存和檔案系統操作進行了優化。

經濟化資料儲存策略,主要體現在兩個關鍵特性上,只要實現了,其他的增強都會如虎添翼:

  1. 使用最合適的儲存系統儲存對應的資料分塊;
  2. 資料儲存策略對上層應用的侵入性越低越好;

比如HDFS典型場景下使用3副本的策略,一方面是確保資料塊的高可用性,同時多個副本也可以更好地保障資料區域性性的要求,提高資料訪問的吞吐量;為了更好地提供資料服務,硬體環境也會選用相對更好的磁碟;對於早期的大資料實踐而言,標準統一的軟硬體選擇可以提高對新技術棧的推動,但是隨著資料的不斷積累,很多資料的訪問頻率呈現指數級下降,尤其是針對合規檢查的冷資料,不僅僅佔據了生產叢集的大量空間,可能一年到頭都沒有被訪問過一次。這是對資源的極大浪費。

大資料發展的現階段,精細化資料儲存被提上了議程。需要一種分層的儲存體系,在維繫現有計算效能的同時,將溫、冷資料實現對上層應用透明的自主遷移,控制資料儲存與維護的成本。

關鍵特性驗證

通過這篇文章,我們希望可以對經濟化資料儲存策略做一個初步探索,首先我們將先前提到的兩個關鍵特性具象化,然後通過幾組實驗對技術可行性進行一個討論。

關鍵特性一:使用一套儲存系統作為熱資料系統;使用另一套儲存系統作為冷資料系統;

關鍵特性二:統一名稱空間同時相容多套儲存系統,通過統一名稱空間對上層應用提供資料訪問服務;

技術選擇:

  1. 計算引擎: Hive (大部分企業使用者使用SQL引擎作為資料開發工具)
  2. 儲存引擎: HDFS/Ozone (業界常用的Apache生態儲存)
  3. 資料編排引擎: Alluxio (第三方開源元件,相容大部分Apache生態元件)

Hive

Apache Hive ™ 資料倉庫軟體有助於使用 SQL 讀取、寫入和管理駐留在分散式儲存中的大型資料集。結構可以投影到已經儲存的資料上。提供了一個命令列工具和 JDBC 驅動程式允許使用者連線到 Hive。

關於Alluxio

“Alluxio資料編排系統”是全球首個分散式超大規模資料編排系統,孵化於加州大學伯克利分校AMP實驗室。自專案開源以來,已有超過來自300多個組織機構的1200多位貢獻者參與開發。Alluxio能夠在跨叢集、跨區域、跨國家的任何雲中將資料更緊密地編排到接近資料分析和AI/ML應用程式的叢集中,從而向上層應用提供記憶體級別的資料訪問速度。

作為大資料生態系統中的儲存與計算分離技術標準,在阿里雲、騰訊雲、華為雲、金山雲等國內頂級雲廠商服務中得到生產檢驗,是建設企業私有云的基石和核心技術。2021年成立後,先後榮登“中關村國際前沿科技創新大賽大資料與雲端計算領域TOP10”、“2021投資界數字科技VENTURE50”、“科創中國”開源創新榜等多項榜單。

技術可行性研究,我們分兩個階段進行:

階段一:使用同一型別的儲存系統HDFS,實現不同HDFS系統之間的冷熱分層【模擬場景:使用新的HDFS3.0 EC或者用磁碟密集型的機器專門搭建冷資料HDFS】

階段二:使用不同型別的儲存系統,使用HDFS作為熱資料儲存系統;使用Ozone作為冷資料儲存系統 【模擬場景:HDFS負責熱資料/Ozone負責冷資料】

驗證步驟

部署架構

軟體版本:

  1. 計算引擎:Hive 2.3.9
  2. 儲存引擎:Hadoop 2.10.1,Ozone 1.2.1,Alluxio 2.8
  3. 所有元件均為單機模式部署

叢集規劃:

主機

元件

ip-172-31-30-130.us-west-2.compute.internal

Hive、HDFS1

ip-172-31-19-127.us-west-2.compute.internal

HDFS2、Ozone

ip-172-31-17-3.us-west-2.compute.internal

Alluxio

實驗一:基於Alluxio實現跨HDFS的透明資料冷熱分層

## Step 1: 在Hive 中建立庫、分割槽表,預設資料儲存在 HDFS_1 上

create database test location "/user/hive/test.db";
create external table test.test_part(value string) partitioned by (dt string);

建立庫

hive> create database test location '/user/hive/test.db';                                                                                                                                                                 
OK                                                                                                                                                                                                                        
Time taken: 1.697 seconds                                                                                                                                                                                                 
hive>

建立表

hive> create external table test.test_part(value string) partitioned by (dt string);                                                                                                                                      
OK                                                                                                                                                                                                                        
Time taken: 0.607 seconds                                                                                                                                                                                                 
hive>

## Step 2: Alluxio Union URI 實現跨HDFS叢集統一名稱空間整合

alluxio fs mount \
--option alluxio-union.hdfs1.uri=hdfs://namenode_1:8020/user/hive/test.db/test_part \
--option alluxio-union.hdfs2.uri=hdfs://namenode_2:8020/user/hive/test.db/test_part \
--option alluxio-union.priority.read=hdfs1,hdfs2 \
--option alluxio-union.collection.create=hdfs1 \
/user/hive/test.db/test_part union://test_part/

以Alluxio Union URI 方式掛載測試目錄

[[email protected] ~]# alluxio fs mkdir /user/hive/test.db                                                                                                                                                           
Successfully created directory /user/hive/test.db                                                                                                                                                                         
[[email protected] conf]# alluxio fs mount \                                                                                                                                                                            
> --option alluxio-union.hdfs1.uri=hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/test.db/test_part \                                                                                                  
> --option alluxio-union.hdfs2.uri=hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part \                                                                                                  
> --option alluxio-union.priority.read=hdfs1,hdfs2 \                                                                                                                                                                      
> --option alluxio-union.collection.create=hdfs1 \                                                                                                                                                                        
> /user/hive/test.db/test_part union://test_part/                                                                                                                                                                         
Mounted union://test_part/ at /user/hive/test.db/test_part                                                                                                                                                                
[[email protected] ~]#

## Step 3: 修改 Hive 表路徑為 Union URI 路徑,遮蔽跨異構儲存的技術細節

alter table test.test_part set location "alluxio://alluxio:19998/user/hive/test.db/test_part";

修改Hive表格對應的路徑

hive> alter table test.test_part set location "alluxio://ip-172-31-17-3.us-west-2.compute.internal:19998/user/hive/test.db/test_part";                                                                                    
OK                                                                                                                                                                                                                        
Time taken: 0.143 seconds                                                                                                                                                                                                 
hive>

## Step 4: 模擬資料

mkdir dt\=2022-06-0{1..6}
echo 1abc > dt\=2022-06-01/000000_0
echo 2def > dt\=2022-06-02/000000_0
echo 3ghi > dt\=2022-06-03/000000_0
echo 4jkl > dt\=2022-06-04/000000_0
echo 5mno > dt\=2022-06-05/000000_0
echo 6pqr > dt\=2022-06-06/000000_0
hdfs dfs -put dt\=2022-06-0{1..3} hdfs://namenode_1:8020/user/hive/test.db/test_part
hdfs dfs -put dt\=2022-06-0{4..6} hdfs://namenode_2:8020/user/hive/test.db/test_part



[[email protected] ~]# mkdir dt\=2022-06-0{1..6}                                                                                                                                                                        
[[email protected] ~]# echo 1abc > dt\=2022-06-01/000000_0                                                                                                                                                              
[[email protected] ~]# echo 2def > dt\=2022-06-02/000000_0                                                                                                                                                              
[[email protected] ~]# echo 3ghi > dt\=2022-06-03/000000_0                                                                                                                                                              
[[email protected] ~]# echo 4jkl > dt\=2022-06-04/000000_0                                                                                                                                                              
[[email protected] ~]# echo 5mno > dt\=2022-06-05/000000_0                                                                                                                                                              
[[email protected] ~]# echo 6pqr > dt\=2022-06-06/000000_0

將模擬資料分別存入hdfs1、hdfs2

[[email protected] ~]# hdfs dfs -put dt\=2022-06-0{1..3} hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/test.db/test_part                                                                            
[[email protected] ~]# hdfs dfs -mkdir -p hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part                                                                                           
[[email protected] ~]# hdfs dfs -put dt\=2022-06-0{4..6} hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part

查詢hdfs1和hdfs2,確認資料存入完成

[[email protected] ~]# hdfs dfs -ls hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/test.db/test_part                                                                                                 
Found 3 items                                                                                                                                                                                                             
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 08:09 hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/test.db/test_part/dt=2022-06-01                                                          
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 08:09 hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/test.db/test_part/dt=2022-06-02                                                          
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 08:09 hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/test.db/test_part/dt=2022-06-03                                                          
[[email protected] ~]# hdfs dfs -ls hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part                                                                                                 
Found 3 items                                                                                                                                                                                                             
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 08:10 hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part/dt=2022-06-04                                                          
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 08:10 hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part/dt=2022-06-05                                                          
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 08:10 hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part/dt=2022-06-06

通過查詢alluxio Union URI 再次確認資料存入hdfs1和hdfs2,以及Union URI跨儲存關聯生效

[[email protected] ~]# alluxio fs ls /user/hive/test.db/test_part                                                                                                                                                       
drwxr-xr-x  root           hdfsadmingroup               1       PERSISTED 07-13-2022 08:09:19:243  DIR /user/hive/test.db/test_part/dt=2022-06-02                                                                         
drwxr-xr-x  root           hdfsadmingroup               1       PERSISTED 07-13-2022 08:09:19:219  DIR /user/hive/test.db/test_part/dt=2022-06-01                                                                         
drwxr-xr-x  root           hdfsadmingroup               1       PERSISTED 07-13-2022 08:10:49:740  DIR /user/hive/test.db/test_part/dt=2022-06-06                                                                         
drwxr-xr-x  root           hdfsadmingroup               1       PERSISTED 07-13-2022 08:10:49:721  DIR /user/hive/test.db/test_part/dt=2022-06-05                                                                         
drwxr-xr-x  root           hdfsadmingroup               1       PERSISTED 07-13-2022 08:10:49:698  DIR /user/hive/test.db/test_part/dt=2022-06-04                                                                         
drwxr-xr-x  root           hdfsadmingroup               1       PERSISTED 07-13-2022 08:09:19:263  DIR /user/hive/test.db/test_part/dt=2022-06-03                                                                         
[[email protected] ~]#

## Step 5: 重新整理Hive表元資料

MSCK REPAIR TABLE test.test_part;

hive> MSCK REPAIR TABLE test.test_part;                                                                                                                                                                                   
OK                                                                                                                                                                                                                        
Partitions not in metastore:    test_part:dt=2022-06-01 test_part:dt=2022-06-02 test_part:dt=2022-06-03 test_part:dt=2022-06-04 test_part:dt=2022-06-05 test_part:dt=2022-06-06                                           
Repair: Added partition to metastore test.test_part:dt=2022-06-01                                                                                                                                                         
Repair: Added partition to metastore test.test_part:dt=2022-06-02                                                                                                                                                         
Repair: Added partition to metastore test.test_part:dt=2022-06-03                                                                                                                                                         
Repair: Added partition to metastore test.test_part:dt=2022-06-04                                                                                                                                                         
Repair: Added partition to metastore test.test_part:dt=2022-06-05                                                                                                                                                         
Repair: Added partition to metastore test.test_part:dt=2022-06-06                                                                                                                                                         
Time taken: 1.677 seconds, Fetched: 7 row(s)

通過select方式觀察到Hive元資料重新整理後,alluxio union URI關聯生效體現到Hive表中

hive> select * from test.test_part;                                                                                                                                                                                       
OK                                                                                                                                                                                                                        
1abc    2022-06-01                                                                                                                                                                                                        
2def    2022-06-02                                                                                                                                                                                                        
3ghi    2022-06-03                                                                                                                                                                                                        
4jkl    2022-06-04                                                                                                                                                                                                        
5mno    2022-06-05                                                                                                                                                                                                        
6pqr    2022-06-06                                                                                                                                                                                                        
Time taken: 1.624 seconds, Fetched: 6 row(s)                                                                                                                                                                              
hive>

## Step 6: 配置冷熱自動分層策略

alluxio fs policy add /user/hive/test.db/test_part "ufsMigrate(olderThan(2m), UFS[hdfs1]:REMOVE, UFS[hdfs2]:STORE)"

設定策略:冷資料(本例中按生成超過2分鐘的資料)自動從熱儲存(hdfs1)遷移到冷儲存(hdfs2)

[[email protected] ~]# alluxio fs policy add /user/hive/test.db/test_part "ufsMigrate(olderThan(2m), UFS[hdfs1]:REMOVE, UFS[hdfs2]:STORE)"                                                                              
Policy ufsMigrate-/user/hive/test.db/test_part is added to /user/hive/test.db/test_part.

通過Alluxio命令列檢視策略設定成功與否

[[email protected] ~]# alluxio fs policy list                                                                                                                                                                           
id: 1657700423909                                                                                                                                                                                                         
name: "ufsMigrate-/user/hive/test.db/test_part"                                                                                                                                                                           
path: "/user/hive/test.db/test_part"                                                                                                                                                                                      
created_at: 1657700423914                                                                                                                                                                                                 
scope: "RECURSIVE"                                                                                                                                                                                                        
condition: "olderThan(2m)"                                                                                                                                                                                                
action: "DATA(UFS[hdfs1]:REMOVE, UFS[hdfs2]:STORE)"                                                                                                                                                                                                                                                                                                                                                                                
[[email protected] ~]#

策略生效後分別檢視hdfs1和hdfs2,可以觀察到hdfs1裡面超過2分鐘的資料都遷移到hdfs2中

[[email protected] logs]# hdfs dfs -ls hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/test.db/test_part                                                                                              
[[email protected] logs]# hdfs dfs -ls hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part                                                                                              
Found 6 items                                                                                                                                                                                                             
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 08:26 hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part/dt=2022-06-01                                                          
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 08:26 hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part/dt=2022-06-02                                                          
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 08:26 hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part/dt=2022-06-03                                                          
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 08:10 hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part/dt=2022-06-04                                                          
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 08:10 hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part/dt=2022-06-05                                                          
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 08:10 hdfs://ip-172-31-19-127.us-west-2.compute.internal:8020/user/hive/test.db/test_part/dt=2022-06-06                                                          
[[email protected] logs]#

策略生效,冷資料自動遷移過程中和完成後查Hive都得到如下預期查詢結果:

hive> select * from test.test_part;                                                                                                                                                                                       
OK                                                                                                                                                                                                                        
1abc    2022-06-01                                                                                                                                                                                                        
2def    2022-06-02                                                                                                                                                                                                        
3ghi    2022-06-03                                                                                                                                                                                                        
4jkl    2022-06-04                                                                                                                                                                                                        
5mno    2022-06-05                                                                                                                                                                                                        
6pqr    2022-06-06                                                                                                                                                                                                        
Time taken: 0.172 seconds, Fetched: 6 row(s)                                                                                                                                                                              
hive>

最後,我們將實驗一的兩個步驟:(1)通過Alluxio的Union URI實現跨兩個HDFS儲存系統的Hive表的資料聯邦,和(2)通過Alluxio實現跨兩個HDFS儲存系統的透明資料冷熱分層,在圖1和圖2中分別以簡化示意圖的方式展示,便於更好的理解實驗目標、過程和結果。

圖1:通過Alluxio的Union URI實現跨兩個HDFS儲存系統的Hive表的資料聯邦的示意圖

圖2:通過Alluxio實現跨兩個HDFS儲存系統的透明資料冷熱分層示意圖

下一組實驗只是將上一組實驗設定中的兩個HDFS儲存系統更改成了兩個異構儲存系統HDFS(熱儲存)和Ozone(冷儲存),從透明冷熱分層功能層面效果是相同的。

實驗二:基於Alluxio實現跨異構儲存(HDFS和Ozone)的透明資料冷熱分層

## step 1 : hive 建立庫、表

create database hdfsToOzone location '/user/hive/hdfsToOzone.db';
create external table hdfsToOzone.test(value string) partitioned by (dt string);

建立庫

hive> create database hdfsToOzone location '/user/hive/hdfsToOzone.db';                                                                                                                                                   
OK                                                                                                                                                                                                                        
Time taken: 0.055 seconds                                                                                                                                                                                                 
hive>

建立表

hive> create external table hdfsToOzone.test(value string) partitioned by (dt string);                                                                                                                                    
OK                                                                                                                                                                                                                        
Time taken: 0.1 seconds                                                                                                                                                                                                   
hive>

## step 2: Alluxio Union URI實現跨HDFS/Ozone叢集統一名稱空間整合

alluxio fs mount \
--option alluxio-union.hdfs.uri=hdfs://HDFS1:8020/user/hive/hdfsToOzone.db/test \
--option alluxio-union.ozone.uri=o3fs://bucket.volume/hdfsToOzone.db/test \
--option alluxio-union.priority.read=hdfs,ozone \
--option alluxio-union.collection.create=hdfs \
--option alluxio.underfs.hdfs.configuration=/mnt1/ozone-1.2.1/etc/hadoop/ozone-site.xml \
/user/hive/hdfsToOzone.db/test union://HDFS_TO_OZONE/

在Ozone中使用命令列工具建立volume、bucket

[[email protected] ~]# ozone sh volume create /v-alluxio                                                                                                                                                      
[[email protected] ~]# ozone sh bucket create /v-alluxio/b-alluxio 
[[email protected] ~]# ozone fs -mkdir -p o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test                                                                                                                      
[[email protected] ~]#

先在Alluxio中建立實驗目錄,然後以 Union URI 方式掛載目錄

[[email protected] ~]# alluxio fs mkdir /user/hive/hdfsToOzone.db
Successfully created directory /user/hive/hdfsToOzone.db
[[email protected] ~]# alluxio fs mount \                                                                                                                                                                               
> --option alluxio-union.hdfs.uri=hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/hdfsToOzone.db/test \                                                                                                 
> --option alluxio-union.ozone.uri=o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test \                                                                                                                                       
> --option alluxio-union.priority.read=hdfs,ozone \                                                                                                                                                                       
> --option alluxio-union.collection.create=hdfs \                                                                                                                                                                         
> --option alluxio.underfs.hdfs.configuration=/mnt1/ozone-1.2.1/etc/hadoop/ozone-site.xml \                                                                                                                               
> /user/hive/hdfsToOzone.db/test union://HDFS_TO_OZONE/                                                                                                                                                                   
Mounted union://HDFS_TO_OZONE/ at /user/hive/hdfsToOzone.db/test                                                                                                                                                          
[[email protected] ~]#

## step 3: 修改 Hive 表路徑為 Union URI 路徑,遮蔽跨異構儲存的技術細節

alter table hdfsToOzone.test set location "alluxio://alluxio:19998/user/hive/hdfsToOzone.db/test";

修改Hive表格對應的路徑

hive> alter table hdfsToOzone.test set location "alluxio://ip-172-31-17-3.us-west-2.compute.internal:19998/user/hive/hdfsToOzone.db/test";                                                                                                           
OK                                                                                                                                                                                                                        
Time taken: 1.651 seconds                                                                                                                                                                                                 
hive>

## step 4: 模擬資料

ozone fs -put dt\=2022-06-0{1..3} o3fs://b-alluxio.v-alluxio.ozone:9862/hdfsToOzone.db/test 
hdfs dfs -put dt\=2022-06-0{4..6} hdfs://HDFS1:8020/user/hive/hdfsToOzone.db/test

將資料存入ozone

[[email protected] ~]# ozone fs -put dt\=2022-06-0{1..3} o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test                                                                                                                 
2022-07-13 10:00:38,920 [main] INFO impl.MetricsConfig: Loaded properties from hadoop-metrics2.properties                                                                                                                 
2022-07-13 10:00:38,981 [main] INFO impl.MetricsSystemImpl: Scheduled Metric snapshot period at 10 second(s).                                                                                                             
2022-07-13 10:00:38,981 [main] INFO impl.MetricsSystemImpl: XceiverClientMetrics metrics system started                                                                                                                   
2022-07-13 10:00:39,198 [main] INFO metrics.MetricRegistries: Loaded MetricRegistries class org.apache.ratis.metrics.impl.MetricRegistriesImpl

通過命令列查詢ozone,確認資料存入完成

[[email protected] ~]# ozone fs -ls o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test                                                                                                                                      
Found 3 items                                                                                                                                                                                                             
drwxrwxrwx   - root root          0 2022-07-13 10:00 o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test/dt=2022-06-01                                                                                                         
drwxrwxrwx   - root root          0 2022-07-13 10:00 o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test/dt=2022-06-02                                                                                                         
drwxrwxrwx   - root root          0 2022-07-13 10:00 o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test/dt=2022-06-03                                                                                                         
[[email protected] ~]#

將資料存入hdfs1,並通過命令列查詢hdfs1,確認資料存入完成

[[email protected] ~]# hdfs dfs -put dt\=2022-06-0{4..6} hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/hdfsToOzone.db/test                                                                          
[[email protected] ~]# hdfs dfs -ls hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/hdfsToOzone.db/test                                                                                               
Found 3 items                                                                                                                                                                                                             
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 10:06 hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/hdfsToOzone.db/test/dt=2022-06-04                                                        
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 10:06 hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/hdfsToOzone.db/test/dt=2022-06-05                                                        
drwxr-xr-x   - root hdfsadmingroup          0 2022-07-13 10:06 hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/hdfsToOzone.db/test/dt=2022-06-06                                                        
[[email protected] ~]#

通過Alluxio命令列查詢,再次確認資料存入hdfs1和ozone,以及Union URI跨儲存關聯生效

[[email protected] ~]# alluxio fs ls /user/hive/hdfsToOzone.db/test                                                                                                                                                     
drwxrwxrwx  root           root                         0       PERSISTED 07-13-2022 10:00:40:670  DIR /user/hive/hdfsToOzone.db/test/dt=2022-06-02                                                                       
drwxrwxrwx  root           root                         0       PERSISTED 07-13-2022 10:00:38:691  DIR /user/hive/hdfsToOzone.db/test/dt=2022-06-01                                                                       
drwxr-xr-x  root           hdfsadmingroup               0       PERSISTED 07-13-2022 10:06:29:206  DIR /user/hive/hdfsToOzone.db/test/dt=2022-06-06                                                                       
drwxr-xr-x  root           hdfsadmingroup               0       PERSISTED 07-13-2022 10:06:29:186  DIR /user/hive/hdfsToOzone.db/test/dt=2022-06-05                                                                       
drwxr-xr-x  root           hdfsadmingroup               0       PERSISTED 07-13-2022 10:06:29:161  DIR /user/hive/hdfsToOzone.db/test/dt=2022-06-04                                                                       
drwxrwxrwx  root           root                         0       PERSISTED 07-13-2022 10:00:40:762  DIR /user/hive/hdfsToOzone.db/test/dt=2022-06-03                                                                       
[[email protected] ~]#

## step 5: 重新整理Hive表元資料

MSCK REPAIR TABLE hdfsToOzone.test;

hive> MSCK REPAIR TABLE hdfsToOzone.test;                                                                                                                                                                                 
OK                                                                                                                                                                                                                        
Partitions not in metastore:    test:dt=2022-06-01      test:dt=2022-06-02      test:dt=2022-06-03      test:dt=2022-06-04      test:dt=2022-06-05      test:dt=2022-06-06                                                
Repair: Added partition to metastore hdfsToOzone.test:dt=2022-06-01                                                                                                                                                       
Repair: Added partition to metastore hdfsToOzone.test:dt=2022-06-02                                                                                                                                                       
Repair: Added partition to metastore hdfsToOzone.test:dt=2022-06-03                                                                                                                                                       
Repair: Added partition to metastore hdfsToOzone.test:dt=2022-06-04                                                                                                                                                       
Repair: Added partition to metastore hdfsToOzone.test:dt=2022-06-05                                                                                                                                                       
Repair: Added partition to metastore hdfsToOzone.test:dt=2022-06-06                                                                                                                                                       
Time taken: 0.641 seconds, Fetched: 7 row(s)                                                                                                                                                                              
hive>

通過select方式觀察到hive元資料重新整理後,alluxio union URI關聯生效體現到hive表中

hive> select * from hdfsToOzone.test ;                                                                                                                                                                                    
OK                                                                                                                                                                                                                        
1abc    2022-06-01                                                                                                                                                                                                        
2def    2022-06-02                                                                                                                                                                                                        
3ghi    2022-06-03                                                                                                                                                                                                        
4jkl    2022-06-04                                                                                                                                                                                                        
5mno    2022-06-05                                                                                                                                                                                                        
6pqr    2022-06-06                                                                                                                                                                                                        
Time taken: 0.156 seconds, Fetched: 6 row(s)                                                                                                                                                                              
hive>

## step 6: 配置策略

alluxio fs policy add /user/hive/hdfsToOzone.db/test" ufsMigrate(olderThan(2m), UFS[hdfs]:REMOVE, UFS[ozone]:STORE)"

設定策略:冷資料(本例中按生成超過2分鐘的資料)自動從熱儲存(hdfs1)遷移到冷儲存(ozone)

[[email protected] ~]# alluxio fs policy add /user/hive/hdfsToOzone.db/test/ "ufsMigrate(olderThan(2m), UFS[hdfs]:REMOVE, UFS[ozone]:STORE)"                                                                            
Policy ufsMigrate-/user/hive/hdfsToOzone.db/test is added to /user/hive/hdfsToOzone.db/test.

通過Alluxio命令列檢視策略設定成功與否

[[email protected] ~]# alluxio fs policy list                                                                                                                                                                           
id: 1657707130843                                                                                                                                                                                                         
name: "ufsMigrate-/user/hive/hdfsToOzone.db/test"                                                                                                                                                                         
path: "/user/hive/hdfsToOzone.db/test"                                                                                                                                                                                    
created_at: 1657707130843                                                                                                                                                                                                 
scope: "RECURSIVE"                                                                                                                                                                                                        
condition: "olderThan(2m)"                                                                                                                                                                                                
action: "DATA(UFS[hdfs]:REMOVE, UFS[ozone]:STORE)"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
[[email protected] ~]#

策略生效後分別檢視hdfs1和ozone,可以觀察到hdfs1裡面超過2分鐘的資料都遷移到ozone中

[[email protected] ~]# ozone fs -ls o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test                                                                                                                                      
Found 6 items                                                                                                                                                                                                             
drwxrwxrwx   - root root          0 2022-07-13 10:00 o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test/dt=2022-06-01                                                                                                         
drwxrwxrwx   - root root          0 2022-07-13 10:00 o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test/dt=2022-06-02                                                                                                         
drwxrwxrwx   - root root          0 2022-07-13 10:00 o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test/dt=2022-06-03                                                                                                         
drwxrwxrwx   - root root          0 2022-07-13 10:21 o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test/dt=2022-06-04                                                                                                         
drwxrwxrwx   - root root          0 2022-07-13 10:21 o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test/dt=2022-06-05                                                                                                         
drwxrwxrwx   - root root          0 2022-07-13 10:21 o3fs://b-alluxio.v-alluxio/hdfsToOzone.db/test/dt=2022-06-06                                                                                                         
[[email protected] ~]# hdfs dfs -ls hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/user/hive/hdfsToOzone.db/test                                                                                               
[[email protected] ~]#

策略生效,冷資料自動遷移過程中和完成後查hive都得到如下預期查詢結果:

hive> select * from hdfsToOzone.test ;                                                                                                                                                                                    
OK                                                                                                                                                                                                                        
1abc    2022-06-01                                                                                                                                                                                                        
2def    2022-06-02                                                                                                                                                                                                        
3ghi    2022-06-03                                                                                                                                                                                                        
4jkl    2022-06-04                                                                                                                                                                                                        
5mno    2022-06-05                                                                                                                                                                                                        
6pqr    2022-06-06                                                                                                                                                                                                        
Time taken: 0.144 seconds, Fetched: 6 row(s)                                                                                                                                                                              
hive>

4. 實驗小結

可以看出,實驗二的執行過程和效果展示和實驗一幾乎是如出一轍,除了冷資料儲存系統從hdfs2切換成了一個異構儲存系統Ozone。

通過實驗,我們充分驗證了Alluxio資料編排是如何成功將上層應用 (比如基於Hive的數倉建設) 與底層資料持久化策略 (使用hdfs或者Ozone, 是否進行冷熱分層等) 解耦合的。同時也體現了Alluxio對於異構儲存系統的通用性和易用性。

最後希望這篇文章對各位如何使用Alluxio經濟化資料儲存策略有所啟迪。

附錄

Alluxio整合Hive及HDFS的方式

Alluxio 配置

echo 'export ALLX_HOME=/mnt1/alluxio' >> ~/.bashrc
echo 'export PATH=$PATH:$ALLX_HOME/bin' >> ~/.bashrc

alluxio.master.hostname=ip-172-31-17-3.us-west-2.compute.internal                                                                                                                                                         
alluxio.underfs.address=hdfs://ip-172-31-30-130.us-west-2.compute.internal:8020/alluxio 
alluxio.worker.tieredstore.level0.dirs.path=/alluxio/ramdisk

alluxio.worker.memory.size=4G
alluxio.worker.tieredstore.levels=1
alluxio.worker.tieredstore.level0.alias=MEM
alluxio.user.file.readtype.default=CACHE
alluxio.user.file.writetype.default=ASYNC_THROUGH
alluxio.security.login.impersonation.username=_HDFS_USER_
alluxio.master.security.impersonation.yarn.groups=*
alluxio.master.security.impersonation.hive.groups=*
alluxio.user.metrics.collection.enabled=true
alluxio.user.block.size.bytes.default=64MB

######## Explore ########
alluxio.user.block.write.location.policy.class=alluxio.client.block.policy.DeterministicHashPolicy
alluxio.user.ufs.block.read.location.policy=alluxio.client.block.policy.DeterministicHashPolicy
alluxio.user.ufs.block.read.location.policy.deterministic.hash.shards=1
alluxio.user.file.persist.on.rename=true
alluxio.master.persistence.blacklist=.staging,_temporary,.tmp
alluxio.user.file.passive.cache.enabled=false

Hive 客戶端core-site.xml

cp /hadoop_home/etc/hadoop/core-site.xml /hive_home/conf

## 拷貝 jar分別到hadoop和hive home下的lib子目錄中

cp /<PATH_TO_ALLUXIO>/client/alluxio-enterprise-2.8.0-1.0-client.jar /hadoop_home/share/lib 
cp /<PATH_TO_ALLUXIO>/client/alluxio-enterprise-2.8.0-1.0-client.jar /hive_home/lib

## 配置alluxio檔案系統

vim /hive_home/conf/core-site.xml 
<property>
   <name>fs.alluxio.impl</name>
   <value>alluxio.hadoop.FileSystem</value>
</property>
<property>
   <name>alluxio.master.rpc.addresses</name>
   <value>ip-172-31-17-3.us-west-2.compute.internal:19998</value>
</property>

HDFS授權

## 檢視hdfs 超級使用者

vim /hadoop_home/etc/hadoop/hdfs-site.xml
<property>
<name>dfs.permissions.superusergroup</name>
<value>hdfsadmingroup</value>
</property>

## 將使用者 Alluxio 增加到supergroup

groupadd hdfsadmingroup
usermod -a -G hdfsadmingroup root

## 同步系統的許可權資訊到 HDFS

su - hdfs -s /bin/bash -c "hdfs dfsadmin -refreshUserToGroupsMappings"

## 開啟hdfs acl

vim /hadoop_home/etc/hadoop/hdfs-site.xml
<property>
<name>dfs.permissions.enabled</name>
<value>true</value>
</property>
<property>
<name>dfs.namenode.acls.enabled</name>
<value>true</value>
</property> 
su - hdfs -s /bin/bash -c "hdfs dfs -setfacl -R -m user:root:rwx /"

Ozone 部署

配置檔案

wget https://dlcdn.apache.org/ozone/1.2.1/ozone-1.2.1.tar.gz 
echo 'export OZONE_HOME=/mnt1/ozone-1.2.1' >> ~/.bashrc
echo 'export PATH=$PATH:$OZONE_HOME/bin:$OZONE_HOME/sbin' >> ~/.bashrc

在ozone-site.xml中加入必要配置資訊

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration>
<property>
<name>ozone.om.address</name>
<value>ip-172-31-19-127.us-west-2.compute.internal:9862</value>
</property>
<property>
<name>ozone.metadata.dirs</name>
<value>/mnt/ozone-1.2.1/metadata/ozone</value>
</property>
<property>
<name>ozone.scm.client.address</name>
<value>ip-172-31-19-127.us-west-2.compute.internal:9860</value>
</property>
<property>
<name>ozone.scm.names</name>
<value>ip-172-31-19-127.us-west-2.compute.internal</value>
</property>
<property>
<name>ozone.scm.datanode.id.dir</name>
<value>/mnt/ozone-1.2.1/metadata/ozone/node</value>
</property>
<property>
<name>ozone.om.db.dirs</name>
<value>/mnt/ozone-1.2.1/metadata/ozone/omdb</value>
</property>
<property>
<name>ozone.scm.db.dirs</name>
<value>/mnt/ozone-1.2.1/metadata/ozone/scmdb</value>
</property>
<property>
<name>hdds.datanode.dir</name>
<value>/mnt/ozone-1.2.1/datanode/data</value>
</property>
<property>
<name>ozone.om.ratis.enable</name>
<value>false</value>
</property>
<property>
<name>ozone.om.http-address</name>
<value>ip-172-31-19-127.us-west-2.compute.internal:9874</value>
</property>
<property>
<name>ozone.s3g.domain.name</name>
<value>s3g.internal</value>
</property>
<property>
<name>ozone.replication</name>
<value>1</value>
</property>
</configuration>

初始化與啟動(按照順序)

ozone scm --init
ozone --daemon start scm
ozone om --init
ozone --daemon start om 
ozone --daemon start datanode
ozone --daemon start s3g

ozone使用操作

建立名稱為v-alluxio的volume

[[email protected] ~]# ozone sh volume create /v-alluxio                                                                                                                                                      
[[email protected] ~]#

在v-alluxio下建立名為b-alluxio的bucket

[[email protected] ~]# ozone sh bucket create /v-alluxio/b-alluxio 
[[email protected] ~]#

檢視bucket的相關資訊

[[email protected] ~]# ozone sh bucket info /v-alluxio/b-alluxio                                                                                                                                                        
{                                                                                                                                                                                                                         
 "metadata" : { },                                                                                                                                                                                                       
 "volumeName" : "v-alluxio",                                                                                                                                                                                             
 "name" : "b-alluxio",                                                                                                                                                                                                   
 "storageType" : "DISK",                                                                                                                                                                                                 
 "versioning" : false,                                                                                                                                                                                                   
 "usedBytes" : 30,                                                                                                                                                                                                       
 "usedNamespace" : 6,                                                                                                                                                                                                    
 "creationTime" : "2022-07-13T09:11:37.403Z",                                                                                                                                                                            
 "modificationTime" : "2022-07-13T09:11:37.403Z",                                                                                                                                                                        
 "quotaInBytes" : -1,                                                                                                                                                                                                    
 "quotaInNamespace" : -1,                                                                                                                                                                                                
 "bucketLayout" : "LEGACY"                                                                                                                                                                                               
}                                                                                                                                                                                                                         
[[email protected] ~]#

建立key,並放入相應的內容

[[email protected] ~]# touch Dockerfile                                                                                                                                                                                 
[[email protected] ~]# ozone sh key put /v-alluxio/b-alluxio/Dockerfile Dockerfile                                                                                                                                      
[[email protected] ~]#

列出bucket下所有的key

[[email protected] ~]# ozone sh key list /v-alluxio/b-alluxio/
{                                                                                                                                                                                                                         
 "volumeName" : "v-alluxio",                                                                                                                                                                                             
 "bucketName" : "b-alluxio",                                                                                                                                                                                             
 "name" : "Dockerfile",                                                                                                                                                                                                  
 "dataSize" : 0,                                                                                                                                                                                                         
 "creationTime" : "2022-07-13T14:37:09.761Z",                                                                                                                                                                            
 "modificationTime" : "2022-07-13T14:37:09.801Z",                                                                                                                                                                        
 "replicationConfig" : {                                                                                                                                                                                                 
   "replicationFactor" : "ONE",                                                                                                                                                                                          
   "requiredNodes" : 1,                                                                                                                                                                                                  
   "replicationType" : "RATIS"                                                                                                                                                                                           
 },                                                                                                                                                                                                                      
 "replicationFactor" : 1,                                                                                                                                                                                                
 "replicationType" : "RATIS"                                                                                                                                                                                             
}
[[email protected] ~]#

檢視key的相關資訊

[[email protected] ~]# ozone sh key info /v-alluxio/b-alluxio/Dockerfile                                                                                                                                                
{                                                                                                                                                                                                                         
 "volumeName" : "v-alluxio",                                                                                                                                                                                             
 "bucketName" : "b-alluxio",                                                                                                                                                                                             
 "name" : "Dockerfile",                                                                                                                                                                                                  
 "dataSize" : 0,                                                                                                                                                                                                         
 "creationTime" : "2022-07-13T14:37:09.761Z",                                                                                                                                                                            
 "modificationTime" : "2022-07-13T14:37:09.801Z",                                                                                                                                                                        
 "replicationConfig" : {                                                                                                                                                                                                 
   "replicationFactor" : "ONE",                                                                                                                                                                                          
   "requiredNodes" : 1,                                                                                                                                                                                                  
   "replicationType" : "RATIS"                                                                                                                                                                                           
 },                                                                                                                                                                                                                      
 "ozoneKeyLocations" : [ ],                                                                                                                                                                                              
 "metadata" : { },                                                                                                                                                                                                       
 "replicationFactor" : 1,                                                                                                                                                                                                
 "replicationType" : "RATIS"                                                                                                                                                                                             
}                                                                                                                                                                                                                         
[[email protected] ~]#

Alluxio 掛載 ozone

方式一

[[email protected] ~]# alluxio fs mount /ozone o3fs://b-alluxio.v-alluxio.ip-172-31-19-127.us-west-2.compute.internal:9862/                                                                                                                          
Mounted o3fs://b-alluxio.v-alluxio.ip-172-31-19-127.us-west-2.compute.internal:9862/ at /ozone                                                                                                                                                         
[[email protected] ~]#

方式二(帶option的mount)

[[email protected] ~]# alluxio fs mount \                                                                                                                                                                               
> --option alluxio.underfs.hdfs.configuration=/mnt1/ozone-1.2.1/etc/hadoop/ozone-site.xml \                                                                                                                               
> /ozone1 o3fs://b-alluxio.v-alluxio/                                                                                                                                                                                     
Mounted o3fs://b-alluxio.v-alluxio/ at /ozone1                                                                                                                                                                            
[[email protected] ~]#

驗證Ozone掛載是否成功

[[email protected] ~]# alluxio fs ls /                                                                                                                                                                                                                                                                          
drwxrwxrwx  root           root                         0       PERSISTED 01-01-1970 00:00:00:000  DIR /ozone1                                                                                                                                                                                                                         
drwxrwxrwx  root           root                         0       PERSISTED 01-01-1970 00:00:00:000  DIR /ozone                                                                                                                                                                                                           
[[email protected] ~]#