目錄
-- 創建分區表
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;