0


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

一、介绍:

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

二、使用场景:

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

三、普通表应用:

1、准备工作
createtable db_1.tb_student(
    id  int,
    name string,
    city    string
)row format delimited fieldsterminatedby',';insertinto db_1.tb_student values(1,'张三','beijing'),(2,'李四','beijing'),(3,'王五','beijing'),(4,'妲己','shanghai'),(5,'哪吒','shanghai'),(6,'雷震子','shanghai'),(7,'悟空','guangzhou'),(8,'八戒','guangzhou'),(9,'沙和尚','guangzhou');createtable db_1.tb_student_2(
    id  int,
    name string,
    city    string
)row format delimited fieldsterminatedby',';droptableifexists tb_student_3_part;createtable db_1.tb_student_3_part(
    id  int,
    name string
)
partitioned by(city    string)row format delimited fieldsterminatedby',';
2、插入数据
insertinto 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;insertinto 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 between6and8;select*from tb_student_3_part;

在这里插入图片描述

五、总结

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


本文转载自: https://blog.csdn.net/weixin_45909317/article/details/132722272
版权归原作者 linbokang 所有, 如有侵权,请联系我们删除。

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

还没有评论