使用 NOT IN 子句替代 Hive 查询

Posted

技术标签:

【中文标题】使用 NOT IN 子句替代 Hive 查询【英文标题】:Alternative to Hive Query with NOT IN clause 【发布时间】:2020-02-27 23:20:20 【问题描述】:

我有以下一组蜂巢表:

create table image_additions (
    customer_id STRING,
    image_key STRING,
    image_size STRING
);
create table image_removals (
   customer_id STRING,
   image_key STRING,
   image_size STRING
);
create table images_stored (
   customer_id STRING,
   image_key STRING,
   image_size STRING
);

我想从如下查询运行插入:

insert into images_stored
select ia.customer_id, 
       ia.image_key, 
       ia.image_size 
from image_additions ia 
where ia.image_key not in 
       (select ir.image_key from image_removals ir);

这会产生一个笛卡尔积,而 hive 不允许我运行它。 如何使用替代查询来做到这一点?

【问题讨论】:

可能重复:***.com/questions/20880124/… 可能重复:***.com/questions/44714625/how-to-use-not-in-in-hive 【参考方案1】:

使用左连接 + where is null;

insert into images_stored
select ia.customer_id, 
       ia.image_key, 
       ia.image_size 
from image_additions ia 
     left join image_removals ir on ia.image_key=ir.image_key 
where ir.image_key is null;

使用不存在:

insert into images_stored
select ia.customer_id, 
       ia.image_key, 
       ia.image_size 
from image_additions ia  
where not exists (select 1 from image_removals ir where ia.image_key=ir.image_key);

【讨论】:

是的,使用 NULL 查询很容易左连接(左连接也是你的名字,好狗) 两个都试过了,时间差不多。非常感谢您的帮助。

以上是关于使用 NOT IN 子句替代 Hive 查询的主要内容,如果未能解决你的问题,请参考以下文章