hive数据库分区表数据迁移到另一个分区/数据复制

一、分区表数据迁移到另一个分区表

有个需求,创建一张备份表,将分区表中的数据迁移到备份表中。以下整理一下几种迁移方式。

创建分区表:

create table  user(
		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'
)

创建备份表

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'
)

1.1、方式一

整张表一起迁移,动态创建分区

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

1.2、方式二

按分区字段单个分区循环迁移

insert into user_bak  partition(dt)  select * from user where dt = '2022';
insert into user_bak  partition(dt)  select * from user where dt = '2023';

可以通过以下命令查看表中有多少分区

hive> select distinct(dt) from user;
2022
2023

二、报错解决

若上述的迁移出现问题,可以通过以下方式设置参数后重新尝试。

1)查看参数设置

# 查看分区模式
set hive.exec.dynamic.partition.mode;
# 是否启动动态分区
set hive.exec.dynamic.partition;

2)设置参数

关闭严格分区模式
动态分区模式时是严格模式,也就是至少有一个静态分区。
set hive.exec.dynamic.partition.mode=nonstrict //分区模式,默认nostrict
set hive.exec.dynamic.partition=true //开启动态分区,默认true
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值