Hive動態分區參數配置
- 往hive分區表中插入數據時,如果需要創建的分區很多,比如以表中某個字段進行分區存儲,則需要復制粘貼修改很多sql去執行,效率低。因為hive是批處理系統,所以hive提供了一個動態分區功能,其可以基於查詢參數的位置去推斷分區的名稱,從而建立分區。
使用動態分區表必須配置的參數
- set hive.exec.dynamic.partition =true(默認false),表示開啟動態分區功能;
- set hive.exec.dynamic.partition.mode = nonstrict(默認strict),表示允許所有分區都是動態的,否則必須有靜態分區字段;
動態分區相關調優參數
- set hive.exec.max.dynamic.partitions.pernode=100 (默認100,一般可以設置大一點,比如1000); 表示每個maper或reducer可以允許創建的最大動態分區個數,默認是100,超出則會報錯。
- set hive.exec.max.dynamic.partitions =1000(默認值) ; 表示一個動態分區語句可以創建的最大動態分區個數,超出報錯;
- set hive.exec.max.created.files =10000(默認) 全局可以創建的最大文件個數,超出報錯。
動態分區表實踐
- 默認已經hive的動態分區和nonstrict模式;
1-創建單一字段分區表
create table dpartition(id int ,name string ) partitioned by(ct string );
2-單個字段的動態分區
- 以city建立動態分區
hive> set hive.exec.dynamic.partition=true; #開啟動態分區,默認是false set hive.exec.dynamic.partition.mode=nonstrict; #開啟允許所有分區都是動態的,否則必須要有靜態分區才能使用。 -- 開始抽取 insert overwrite table dpartition partition(ct) select id ,name,city from mytest_tmp2_p;
- 要點:因為dpartition表中只有兩個字段,所以當我們查詢了三個字段時(多了city字段),所以系統默認以最后一個字段city為分區名,因為分區表的分區字段默認也是該表中的字段,且依次排在表中字段的最后面。所以分區需要分區的字段只能放在后面,不能把順序弄錯。如果我們查詢了四個字段的話,則會報錯,因為該表加上分區字段也才三個。要注意系統是根據查詢字段的位置推斷分區名的,而不是字段名稱。
hive>--查看可知,hive已經完成了以city字段為分區字段,實現了動態分區。 hive (fdm_sor)> show partitions dpartition; partition ct=beijing ct=beijing1
- 注意:使用,insert...select 往表中導入數據時,查詢的字段個數必須和目標的字段個數相同,不能多,也不能少,否則會報錯。但是如果字段的類型不一致的話,則會使用null值填充,不會報錯。而使用load data形式往hive表中裝載數據時,則不會檢查。如果字段多了則會丟棄,少了則會null值填充。同樣如果字段類型不一致,也是使用null值填充。
3-多個分區字段,實現半自動分區
1.創建一個只有一個字段,兩個分區字段的分區表
hive (fdm_sor)> create table ds_parttion(id int )
> partitioned by (state string ,ct string );
2.往該分區表半動態分區插入數據
hive>
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table ds_parttion
partition(state='china',ct) #state分區為靜態,ct為動態分區,以查詢的city字段為分區名
select id ,city from mytest_tmp2_p;
3.查詢結果顯示:
hive (fdm_sor)> select * from ds_parttion where state='china'
> ;
ds_parttion.id ds_parttion.state ds_parttion.ct
4 china beijing
3 china beijing
2 china beijing
1 china beijing
4 china beijing1
3 china beijing1
2 china beijing1
1 china beijing1
hive (fdm_sor)> select * from ds_parttion where state='china' and ct='beijing';
ds_parttion.id ds_parttion.state ds_parttion.ct
4 china beijing
3 china beijing
2 china beijing
1 china beijing
hive (fdm_sor)> select * from ds_parttion where state='china' and ct='beijing1';
ds_parttion.id ds_parttion.state ds_parttion.ct
4 china beijing1
3 china beijing1
2 china beijing1
1 china beijing1
Time taken: 0.072 seconds, Fetched: 4 row(s)
4-多個分區字段,全部實現動態分區插入
set hive.exec.dynamic.partition=true; set hive.exec.dynamic.partition.mode=nonstrict; insert overwrite table ds_parttion partition(state,ct) select id ,country,city from mytest_tmp2_p; 注意:字段的個數和順序不能弄錯。
- 注意動態分區字段的順序