insert overwrite table:数据仓库和数据分析中的常用技术

一、介绍:

INSERT OVERWRITE TABLE 是用于覆盖(即替换)目标表中的数据的操作。它将新的数据写入表中,并删除原有的数据。这个操作适用于非分区表和分区表。

二、使用场景:

1、数据更新:当您需要更新表中的数据时,可以使用覆写操作。通过覆写,您可以将新的数据写入表中,替换原有的数据。这在需要定期更新或替换表中数据的情况下非常有用。
2、数据重载:如果您需要重新加载表中的数据,覆写操作可以清空表并将新的数据加载进去。这在数据仓库或数据分析任务中很常见,当需要重新加载或替换表中的数据时,覆写操作是一个快速有效的方法。
3、数据清理:当需要删除表中的数据时,可以使用覆写操作。通过将一个空表覆写到目标表中,可以清空表中的数据并释放存储空间。
注:insert overwrite table 是一个具有破坏性操作的语句,因为它会完全覆盖表中的数据。在使用之前,请确保您理解该操作的影响,并备份重要的数据以防止意外数据丢失。

三、普通表应用:

1、准备工作
create table db_1.tb_student(
    id  int,
    name string,
    city    string
)
row format delimited fields terminated by ',';
insert into db_1.tb_student values
(1,'张三','beijing') ,                                 
(2, '李四', 'beijing'),
(3, '王五', 'beijing'),
(4, '妲己', 'shanghai'),
(5, '哪吒', 'shanghai'),
(6, '雷震子', 'shanghai'),
(7, '悟空', 'guangzhou'),
(8, '八戒', 'guangzhou'),
(9, '沙和尚', 'guangzhou');
create table db_1.tb_student_2(
    id  int,
    name string,
    city    string
)
row format delimited fields terminated by ',';
drop table if exists tb_student_3_part;
create table db_1.tb_student_3_part(
    id  int,
    name string
)
partitioned by (city    string)
row format delimited fields terminated by ',';
2、插入数据
insert into db_1.tb_student_2
select * from tb_student;
select * from db_1.tb_student_2; 

在这里插入图片描述

3、按条件覆写
insert overwrite table db_1.tb_student_2
select * from tb_student
where id>=6
;

在这里插入图片描述

四、分区表应用

1、全部插入
set hive.exec.dynamic.partition.mode=nonstrict;
insert into db_1.tb_student_3_part partition(city)
select * from tb_student
;
select * from tb_student_3_part;

在这里插入图片描述

2、部分插入
insert overwrite table db_1.tb_student_3_part partition(city)
select * from tb_student where id between 6 and 8
;
select * from tb_student_3_part;

在这里插入图片描述

五、总结

1、对于分区表,insert overwrite table 操作会覆盖指定分区的数据,而不会影响其他分区的数据。只有指定的分区会被更新或替换。这样可以实现更精细和高效的数据管理。
2、而对于普通表(即非分区表),insert overwrite table 操作将完全覆盖表中的所有数据,不考虑任何分区。所有的数据将被删除,并被新插入的数据替换。
3、因此,分区表和普通表在 insert overwrite table 操作上的区别在于操作的粒度。分区表仅覆盖指定分区的数据,而普通表覆盖整个表的数据。

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值