1、創(chuàng)建分區(qū)表
--- 創(chuàng)建分區(qū)表
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")
;
-- 動態(tài)插入前設置
set hive.exec.dynamic.partition.mode=nonstrict;
-- 插入數(shù)據(jù)
insert overwrite table temp_base.temp_table partition(dt)
-- overwrite 和 into 的區(qū)別
-- into 會在原分區(qū)增加數(shù)據(jù)
-- overwrite 會全量替換原分區(qū)數(shù)據(jù)
select
-- 注意分區(qū)字段必須在最后一個
-- 表字段跟建表字段順序一致
id,
name,
dt
from 庫名.表名
where dt between '2020-02-01' and '2020-02-04'
;
-- 查看分區(qū)
show partitions temp_base.temp_table
;
-- 查看表信息
show create table temp_base.temp_table;
-- 或者
desc temp_base.temp_table;