hive動態分區概念:允許插入數據到分區表時,根據插入的數據內容自動創建相應的分區
1.啟用動態分區功能
hive.exec.dynamic.partition=true;
2.分區字段設置
在insert語句中, 動態分區的字段必須放在select語句的末尾,hive會根據這個字段的值來創建分區目錄
示例:
--創建分區表:
create table test (
id string
,name string
)
partitioned by (dt string)
row format delimited
fields terminated by '|'
lines terminated by '\n'
;--動態插入數據:
set hive.exec.dynamic.partition=true;
insert into test
select '1' as id, 'one' as name, '20200921' as dt
union all
select '2' as id, 'two' as name, '20200922' as dt
union all
select '3' as id, 'three' as name, '20200923' as dt--查看最終效果:
select * from test;
+----------+------------+-----------+
| test.id | test.name | test.dt |
+----------+------------+-----------+
| 1 | one | 20200921 |
| 2 | two | 20200922 |
| 3 | three | 20200923 |
+----------+------------+-----------+
3 rows selected (0.618 seconds)show partitions test;
+--------------+
| partition |
+--------------+
| dt=20200921 |
| dt=20200922 |
| dt=20200923 |
+--------------+
3 rows selected (0.436 seconds)