Hive如何根據表中某個欄位動態分割槽
阿新 • • 發佈:2018-11-12
使用hive儲存資料時,需要對做分割槽,如果從kafka接收資料,將每天的資料儲存一個分割槽(按天分割槽),儲存分割槽時需要根據某個欄位做動態分割槽,而不是傻傻的將資料寫到某一個臨時目錄最後倒入到某一個分割槽,這是靜態分割槽。
Hive動態分割槽步驟如下:
1、建立某一個源表模擬資料來源並插入一些資料
create table t_test_p_source ( id string, name string, birthday string ) row format delimited fields terminated by '\t' storedas textfile; insert into t_test_p_source values ('a1', 'zhangsan', '2018-01-01'); insert into t_test_p_source values ('a2', 'lisi', '2018-01-02'); insert into t_test_p_source values ('a3', 'zhangsan', '2018-01-03'); insert into t_test_p_source values ('a4', 'wangwu', '2018-01-04'); insert into t_test_p_source values('a5', 'sanzang', '2018-01-05'); insert into t_test_p_source values ('a6', 'zhangsan2', '2018-01-01');
2、建立一張分割槽表 (按ds欄位分割槽)
create table t_test_p_target ( id string, name string ) partitioned by (ds string) row format delimited fields terminated by '\t' stored as textfile;
3、向分割槽表中插入資料
SET hive.exec.dynamic.partition=true; #是否開啟動態分割槽,預設是false,所以必須要設定成true SET hive.exec.dynamic.partition.mode=nonstrict; # 動態分割槽模式,預設為strict, 表示表中必須一個分割槽為靜態分割槽,nostrict表示允許所有欄位都可以作為動態分割槽 insert into table t_test_p_target partition (ds) select id, name, birthday as ds from t_test_p_source;
4、測試是否動態分割槽了
2018-01-01這個分割槽只有2條資料,再來看下HDFS上的分割槽目錄
至此,hive動態分割槽已經完成了。