以Hive中的数据交集 [英] Taking intersection of data in Hive

查看:1052
本文介绍了以Hive中的数据交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在配置单元中填入以下数据:

  userid cityid 
1 15
2 15
1 7
3 15
2 8
3 9
3 7

我只想保留具有cityid 15和cityid 7的用户标识(在我的示例中,它将是用户标识1和3)。
我试过了:

 从表中选择userid,其中cityid = 15并且userid在(从表中选择userid,其中cityid = 7); 

但是在配置单元中它不起作用。
有人可以帮忙吗?

谢谢!

解决方案

尝试避免自加入

  SELECT userid 
FROM
(SELECT userid,collect_set(cityid)as城市
FROM表
GROUP BY userid

WHERE array_contains(cities,7)
AND array_contains(cities,15);


I have in hive the following data:

userid cityid
1      15
2      15
1      7
3      15
2      8
3      9
3      7

And I want to keep only the userid's that have a cityid 15 and a cityid 7 (in my example, it would be userid's 1 and 3). I tried:

select userid from table where cityid = 15 and userid in (select userid from table where cityid = 7);

But with hive it is not working. Can somebody help?

Thanks!

解决方案

Try avoiding a self-join

SELECT  userid
FROM
 ( SELECT userid, collect_set( cityid) as cities
   FROM table 
   GROUP BY userid 
 )
WHERE array_contains( cities, 7 )
AND array_contains( cities, 15 );

这篇关于以Hive中的数据交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆