Hive 動態分割槽 靜態分割槽

2021-07-08 12:20:52 字數 2521 閱讀 3402

本文**:

參考:hive預設是靜態分割槽。但是有時候可能需要動態建立不同的分割槽來區分不同的分類。

hive中建立分割槽表沒有什麼複雜的分割槽型別(範圍分割槽、列表分割槽、hash分割槽、混合分割槽等)。分割槽列也不是表中的乙個實際的字段,而是乙個或者多個偽列。意思是說在表的資料檔案中實際上並不儲存分割槽列的資訊與資料。

工作中使用動態分割槽的例子:

因為要給乙個已經建立的表增加分割槽字段(原表沒有分割槽或者需要多增加幾個分割槽,好多文件的增加分割槽都預設是增加分割槽欄位的值),而從文件中沒有增加分割槽欄位的內容,所以只好做表資料遷移了。

下面是表的sql語句:

舊表結構

create external table db_stat.beauty_day_imei(imei string,mid string,f string,v string,s string,hid string,ip string,openudid string,tuid string,countint,active int)

partitioned by (time string)

row format delimited fields terminated by'`'

stored as textfile;

新錶結構

create external table db_stat.beauty_day_imei2(imei string,mid string,f string,v string,s string,hid string,ip string,openudid string,tuid string,countint,active int)

partitioned by (type string,time string)

row format delimited fields terminated by'`'

stored as textfile;

從舊表轉移資料到新錶

insert overwrite table db_stat.beauty_day_imei2 partition(type='base',time)

select * from db_stat.beauty_day_imei ;

注意上面的time欄位。在舊表(db_stat.beauty_day_imei)中time是分割槽字段,在新錶(db_stat.beauty_day_imei2)中type和time是分割槽字段。time資料直接從舊表中取得,自動作為新錶time的分割槽。而實質儲存的資料中沒用type和time欄位資料。

舊表結構

create external table db_stat.beauty_all_imei(imei string,clicks int,regisday int,modday int,userday int)

partitioned by ( time string)

row format delimited fields terminated by'`'

stored as rcfile;

新錶結構

create external table db_stat.beauty_all_imei2(imei string,clicks int,regisday int,modday int,userday int)

partitioned by (type string, pt string , time string)

row format delimited fields terminated by'`'

stored as rcfile;

從舊表遷移資料到新錶

insert overwrite table db_stat.beauty_all_imei2 partition(type='base',pt='android', time)

select * from db_stat.beauty_all_imei ;

參考:hive從查詢中獲取資料插入到表或動態分割槽

HIVE分割槽,靜態分割槽,動態分割槽

分割槽可以大大提公升hive的效能,這裡就要提到數倉的分層 原始資料層,儲存原始收集的資料 數倉明細層,裡面做的是轉換和分析,裡面包含部分的資料清洗的過程 數倉服務層,對外業務的處理,如維度轉 鍵 身份證清洗 會員註冊 清晰 字段合併 空值處理 髒資料處理 ip清晰轉換等 最終業務層 適合做增量表,...

HIVE 動態分割槽與靜態分割槽

hive分割槽,實際上是通過乙個路徑來標識的,而不是在物理資料中。比如每天的資料,可能分割槽是pt 20121023這樣,那麼路徑中它就會變成 hdfs path pt 20121023 data files。通過路徑來標識的好處是,如果我們需要取特定分割槽的資料,只需要把這個路徑下的資料取出來就可...

hive 動態分割槽和靜態分割槽

hive分割槽,實際上是通過乙個路徑來標識的,而不是在物理資料中。比如每天的資料,可能分割槽是pt 20121023這樣,那麼路徑中它就會變成 hdfs path pt 20121023 data files。通過路徑來標識的好處是,如果我們需要取特定分割槽的資料,只需要把這個路徑下的資料取出來就可...