hive 中 in与not in

在mysql中想表达包含与不包含可以用 in/exists,但是在hive1的版本中是不支持这种语法的,hive2支持这种语法但是效率极低,这里给出一种解决方案。

建表与导入数据

create table if not exists parent(parent_id int,pname string,comments string) row format delimited fields terminated by "\t";
load data local inpath "/home/hadoop/hive_data/parent.txt" into table parent;

数据

parent_id    pname    comments

1    Dan    so what    
2    Jack    who cares
3    Rose    yeah right

建表与导入数据

parent_id    person_id    cname    comments

create table if not exists children(parent_id int,person_id int,cname string,comments string) row format delimited fields terminated by "\t";
load data local inpath "/home/hadoop/hive_data/children.txt" into table children;

数据

1    2    annne    who cares
1    1    julia    yeah right
2    1    marcella    so what
4    3    alice    yeah right

例1:找出父母资料在表中的孩子的名字

in 查询方式

select cname 
from children a left outer join parent b on a.parent_id=b.parent_id
where b.comments is not null;

查询结果

cname
annne
julia
marcella

例2:找出父母资料不在表中的孩子的名字

not in查询方式

select cname 
from children a left outer join parent b on a.parent_id=b.parent_id
where b.comments is null;

查询结果

cname
alice

发布了183 篇原创文章 · 获赞 126 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/young_0609/article/details/103001103