Hive批量刪除一段時間分區和動態分區更新數據
1.hive動態分區
-- 批量刪除分區數據
alter table dm.dm_call_gateway_bill_time_detail drop partition(dt >="20191101",dt <='20191130')
-- 開啓動態分區
SET hive.exec.dynamic.partition=true; --開啓動態分區,默認是false
SET hive.exec.dynamic.partition.mode=nonstrict; --開啓允許所有分區都是動態的,否則必須要有靜態分區才能使用
-- 省略了部分表和字段
insert overwrite table dm.dm_call_gateway_bill_time_detail
partition(dt) --分區字段
SELECT date_format(t.START_TIME,'yyyy-MM-dd') AS p_date,
g.gateway_name,
t.end_time,
-- 以上是表字段
date_format(t.START_TIME,'yyyyMMdd') as dt
--最後一個字段爲動態分區字段
FROM ods_fdm.fdm_call_transaction t -- 全量表
WHERE t.dt = '${p_date}'
AND date_format(t.START_TIME,'yyyyMMdd') >= '20191101'
AND date_format(t.START_TIME,'yyyyMMdd') <= '20191130';
要點:因爲dm.dm_call_gateway_bill_time_detail表中只有3個字段,所以當我們查詢了4個字段時(多了dt字段,名字任意),所以系統默認以最後一個字段dt爲分區名,因爲分區表的分區字段默認也是該表中的字段,且依次排在表中字段的最後面。
所以分區需要分區的字段只能放在最後面,不能把順序弄錯。如果我們查詢5個字段的話,則會報
錯,因爲該表加上分區字段也才4個。要注意系統是根據查詢字段的位置推斷分區名的,而不是字段名稱。
SHOW PARTITIONS dm.dm_call_gateway_bill_time_detail;
dt=20191101
dt=20191102
dt=20191103
....
dt=20191129
dt=20191130
...
2.多個分區字段時,實現半自動分區(部分字段靜態分區,注意靜態分區字段要在動態前面)
1.創建一個只有一個字段,兩個分區字段的分區表
hive (fdm_sor)> create table ds_parttion(id int )
> partitioned by (state string ,ct string );
2.往該分區表半動態分區插入數據
hive>
set hive.exec.dynamici.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)
3.多個分區字段時,全部實現動態分區插入數據
set hive.exec.dynamici.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;
注意:字段的個數和順序不能弄錯
4.動態分區表的屬性
使用動態分區表必須配置的參數 :
set hive.exec.dynamic.partition =true(默認false),表示開啓動態分區功能
set hive.exec.dynamic.partition.mode = nonstrict(默認strict),表示允許所有分區都是動態的,否則必須有靜態分區字段
5.動態分區相關的調優參數
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(默認)
全局可以創建的最大文件個數,超出報錯。
6.小福利:更新增量數據的shell腳本
#!/usr/bin/env bash
start_date="$1"
end_date="$2"
task_job="$3"
while [ ${start_date} -le ${end_date} ]
do
echo "${start_date}"
python /data/apps/${task_job} -d ${start_date}
start_date=`date -d "${start_date} +1 day" +%Y%m%d`
done