1、构建分区表
CREATE table if NOT EXISTS table_name(
x1 string comment ‘指标1’
,x2 string comment ‘指标2’
)
COMMENT ‘分区别测试
partitioned by (pt string comment '分区日期')
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
2、对分区表进行修改
(1)、如:增加新字段,部分用户通过构建新的分区表,然后将旧分区表的数据导入新分区表,删除旧分区表,修改新分区表名称。不建议使用该方法,因为是静态分区表,从旧表导入数据到新表时会报错:
Error while compiling statement: FAILED: SemanticException [Error 10096]: Dynamic partition strict mode requires at least one static partition column. To turn this off set hive.exec.dynamic.partition.mode=nonstrict
建议通过 Alter 方式直接增加原表字段,然后正常运行增加了字段的插入数据代码,插入新分区数据:
Alter table table_name add columns (time_now timestamp comment '跑数时间')
insert into table_name (pt = ‘xxxxx’) ----加上静态分区
select
*
,current_timestamp() as time_now ----新增的时间字段
from table_name_new
此处存在坑:
hive建表后插入数据一定要基于原始的建表字段顺序插入,否则就会出现字段错乱情况,因为hive无法根据字段名称去对应识别插入数据,因为通过alter方式增加的字段放在表的最后位置,所以插入数据时新增字段同样需要放置最后位置。
(2)、如发现插入分区的数据有问题,可通过以下代码删除对应分区数据,重新导入新的数据:
ALTER TABLE table_name DROP PARTITION (pt = 'xxxxx')