在少数情况下,Hive 插入会覆盖会截断表



我正在研究一种解决方案,发现在某些特定情况下,hiveinsert overwrite截断表,但在少数情况下不会。 有人可以解释一下它的行为吗?

为了解释这一点,我是表两个表,源和目标,并尝试使用insert overwrite将数据从表插入主

当源表有分区时

如果源表具有分区,并且您编写的条件使分区不存在,则它不会截断主表。

create table source (name String) partitioned by (age int);
insert into source partition (age) values("gaurang", 11);
create table target (name String, age int);
insert into target partition (age) values("xxx", 99);

以下查询不会截断表,即使 Select 不返回任何内容也是如此。

insert overwrite  table temp.test12 select * from temp.test11 where name="Ddddd" and age=99;

但是,以下查询将截断表

insert overwrite  table temp.target select * from temp.test11 where name="Ddddd" and age=11;

在第一种情况下这是有意义的,因为分区(age=99(不存在,因此它应该进一步停止查询的执行。然而,这是我的假设,不确定到底发生了什么。

当源表没有分区,但在这种情况下,目标有,即使源表中的 select 语句返回 0 行,目标表也不会被截断。

use temp;
drop table if exists source1;
drop table if exists target1;
create table source1 (name String, age int);
create table target1 (name String) partitioned by (age int);
insert into source1 values ("gaurang", 11);
insert into target1 partition(age) values("xxx", 99);
select  * from source1;
select * from target1;

以下查询不会截断表,即使在 select 语句中未找到数据也是如此。

insert overwrite table temp.target1 partition(age) select * from temp.source1 where age=90;

当源或目标没有分区时

在这种情况下,如果我尝试插入覆盖目标并且 select 语句不返回任何行,则目标表将被截断。 检查下面的示例。

use temp;
drop table if exists source1;
drop table if exists target1;
create table source1 (name String, age int);
create table target1 (name String, age int);
insert into source1 values ("gaurang", 11);
insert into target1 values("xxx", 99);
select  * from source1;
select * from target1;

以下查询将截断目标表。

insert overwrite table temp.target1 select * from temp.source1 where age=90;

最好使用术语'overwrite'而不是truncate,因为它是insert overwrite期间发生的事情。

写入overwrite table temp.target1 partition(age)时,指示 Hive 覆盖分区,而不是覆盖所有 target1 表,仅覆盖那些将由 select 返回的分区。

空数据集不会覆盖动态分区模式下的分区。 因为要覆盖的分区是未知的,所以应该从数据集中获取分区,并且数据集是空的,然后没有要覆盖的内容。

并且在没有分区表的情况下,已经知道它应该覆盖所有表,不管是否空数据集。

语句insert overwrite分区列应该是最后一个。而 target 中要覆盖的分区列表 = 分区列中的值列表,由数据集返回,与源表如何分区无关(您可以从任何源表列中选择目标分区列,计算它或使用常量(,只有返回的内容才重要。

最新更新