1>開啓分區
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
否則會出拋出異常:
2>創建分區表
創建靜態分區表:
create table test_part_table(
word string,
num bigint
)partitioned by(dt string)
row format delimited fields terminated by '\t';
--添加分區
alter table test_part_table add if not exists partition(dt='20190808') location '20190808';
--插入數據
load data local INPATH '/home/dongwentao15/dataTest/test1' overwrite into table test_part_table PARTITION (dt='20190811');
hive (dongwentao15)> select * from test_part_table;
OK
dwt 22 20190811
cn 3 20190811
un 1 20190811
fk 5 20190811
pl 19 20190811
6 NULL 20190811
第二步驟的添加分區可以省略,可以直接load數據到分區表中,在load數據的過程中,hive會自動創建分區目錄。
創建動態分區表:
create table orders_part(
order_id string,
user_id string,
eval_set string,
order_number string,
order_hour_of_day string,
days_since_prior_order string
)partitioned by(order_dow string)
row format delimited fields terminated by ',';
--添加數據
insert into table orders_part partition (order_dow) select order_id,user_id,eval_set,order_number,order_hour_of_day,days_since_prior_order,order_dow from orders;
其中orders表中的字段是:
order_id,user_id,eval_set,order_number,order_dow,order_hour_of_day,days_since_prior_order
需要注意的是:動態添加分區的時候,查詢的分區字段必須放在最後面(order_dow),否則結果不是你想要的;
insert...select 往表中導入數據時,查詢的字段個數必須和目標的字段個數相同,不能多,也不能少,否則會報錯。但是如果字段的類型不一致的話,則會使用null值填充,不會報錯。而使用load data形式往hive表中裝載數據時,則不會檢查。如果字段多了則會丟棄,少了則會null值填充。同樣如果字段類型不一致,也是使用null值填充。