hive数据库将非分区表数据迁移到分区表

一、非分区表数据迁移到分区表

业务运行一段时间后非分区表的数据量非常大,需要创建一张分区表并将数据迁移到分区表中。

原表建表语句:

create table  user(
		id  String  default null comment '主键id',
		name String default null comment '姓名',
		birthday String default null comment '出生日期'
) comment '用户表'
stored as orc
location 'hdfs://nameservice1//tmp/wrk/user'
tblproperties(
	'transactional'='true'
)

分区表建表语句:

新建分区表按出生日期进行分区,新增dt字段作为分区字段

create table  user_bak(
		id  String  default null comment '主键id',
		name String default null comment '姓名',
		birthday String default null comment '出生日期'
) comment '用户表'
partitioned by(
	 dt String
)
stored as orc
location 'hdfs://nameservice1//tmp/wrk/user'
tblproperties(
	'transactional'='true'
)

数据迁移

将非分区表的数据迁移到分区表

insert into user_bak  partition(dt)  select *,birthday from user;

若sql执行错误,可在insert语句之前先执行下列这个语句,然后再执行insert语句
set hive.exec.dynamic.partition = true;

猜你喜欢

转载自blog.csdn.net/weixin_49114503/article/details/134462385