Hivesql創建分區表,動態插入數據(同時插入多分區)

目錄

1、創建分區表語法

2、動態插入設置

3、動態插入語法

4、查看分區字段

5、查看錶信息、表字段

 

-- 創建分區表
drop table temp_base.temp_table;
CREATE TABLE IF NOT EXISTS temp_base.temp_table(
id string comment '字段id註釋'
,name string  comment '字段name註釋'
) COMMENT '備註表名'
PARTITIONED BY (`dt` string) 
ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.orc.OrcSerde' 
with serdeproperties('serialization.null.format' = '')
STORED AS ORC
TBLPROPERTIES ("orc.compress"="SNAPPY")
;



-- 動態插入前設置
set hive.exec.dynamic.partition.mode=nonstrict;

-- 插入數據
insert overwrite table temp_base.temp_table partition(dt)
-- overwrite 和 into 的區別
-- into 會在原分區增加數據
-- overwrite 會全量替換原分區數據


select
-- 注意分區字段必須在最後一個
-- 表字段跟建表字段順序一致
	id,
	name,
	dt

from 庫名.表名
where dt between '2020-02-01' and '2020-02-04'
;


-- 查看分區
show partitions temp_base.temp_table
;


-- 查看錶信息
show create table temp_base.temp_table
;

-- 或者
desc  temp_base.temp_table;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章