Hive创建分区表,动态插入数据

1、创建分区表

--- 创建分区表
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;

你可能感兴趣的:(Hive创建分区表,动态插入数据)