如何编写子查询并使用“In” Hive 中的子句

发布于 2024-12-08 10:10:53 字数 267 浏览 0 评论 0原文

如何在 Hive 中使用 In 子句 我想在 Hive 中写这样的东西 从 y 中选择 x,其中 yz 按 x 顺序(从 y 中选择不同的 z); 但我没有找到任何方法可以做到这一点.. 我在 Hive 0.7 中尝试了 In 子句,它抛出错误,我还尝试了 Find_in_Set .. 使用 find_in_set(yz,subquery) ..但是作业失败了。

我想在 Hive 上执行此操作。如果有人知道如何在 Hive 中执行此操作,请帮助我。

谢谢&问候, 阿图尔

How can I use In clause in Hive
I want to write something like this in Hive
select x from y where y.z in (select distinct z from y) order by x;
But I am not finding any way of doing it..
I tried In clause in Hive 0.7 it was throwing error, Also I tried Find_in_Set ..
using find_in_set(y.z,subquery).. but the job is getting failed.

I want to do this on Hive. Please help me if anybody knows how to do this in Hive..

Thanks & Regards,
Atul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

以可爱出名 2024-12-15 10:10:53

您可以使用半连接(https://cwiki.apache.org/Hive/languagemanual-joins.html):

LEFT SEMI JOIN 以有效的方式实现相关的 IN/EXISTS 子查询语义。由于 Hive 目前不支持 IN/EXISTS 子查询,因此您可以使用 LEFT SEMI JOIN 重写查询。使用 LEFT SEMI JOIN 的限制是右侧表只能在连接条件(ON 子句)中引用,而不能在 WHERE 或 SELECT 子句等中引用。

SELECT a.key, a.value
  FROM a
  WHERE a.key in
   (SELECT b.key
    FROM B);

可以重写为:

   SELECT a.key, a.val
   FROM a LEFT SEMI JOIN b on (a.key = b.key)

You can use semi join(https://cwiki.apache.org/Hive/languagemanual-joins.html):

LEFT SEMI JOIN implements the correlated IN/EXISTS subquery semantics in an efficient way. Since Hive currently does not support IN/EXISTS subqueries, you can rewrite your queries using LEFT SEMI JOIN. The restrictions of using LEFT SEMI JOIN is that the right-hand-side table should only be referenced in the join condition (ON-clause), but not in WHERE- or SELECT-clauses etc.

SELECT a.key, a.value
  FROM a
  WHERE a.key in
   (SELECT b.key
    FROM B);

can be rewritten to:

   SELECT a.key, a.val
   FROM a LEFT SEMI JOIN b on (a.key = b.key)
飘逸的'云 2024-12-15 10:10:53

Hive 0.13 现在支持 WHERE 子句中的 IN/EXISTS .. 问题 https:// issues.apache.org/jira/browse/HIVE-784 已在 4 年后得到解决:)

Hive 0.13 now do support IN/EXISTS in the WHERE-clause .. The issue https://issues.apache.org/jira/browse/HIVE-784 has been resolved after 4 years :)

蛮可爱 2024-12-15 10:10:53

我正在使用 hive 版本 0.7.1SELECT * FROM MYTABLE WHERE MYCOLUMN IN ('thisThing','thatThing');

我在列类型上对此进行了测试 STRING 所以我不确定这是否适用于所有数据类型,因为我注意到像上面提到的 Wawrzyniec 一样,Hive 语言手册说不支持它,而是使用 LEFT SEMI JOIN 但在我的测试中效果很好。

I'm using hive version 0.7.1 and SELECT * FROM MYTABLE WHERE MYCOLUMN IN ('thisThing','thatThing');

I tested this on a column type STRING so I am not sure if this works universally on all data types since I noticed like Wawrzyniec mentioned above that the Hive Language Manual says that it is not supported and to instead use LEFT SEMI JOIN but it worked fine in my test.

酒废 2024-12-15 10:10:53

Hive 完美支持 IN ...它不支持 WHERE 子句中的子查询

自 4 年来 Facebook 工程师就有一个开放功能票证...
https://issues.apache.org/jira/browse/HIVE-784 ?focusedCommentId=13579059

Hive supports perfectly the IN ... it does not support the subquery in the WHERE clause

there is an open feature ticket from Facebook engineers since 4 years...
https://issues.apache.org/jira/browse/HIVE-784?focusedCommentId=13579059

绝不放开 2024-12-15 10:10:53

假设 table t1(id,name)table t2(id,name)

仅列出 t2 中存在的 t1 中的 id(基本上 < code>IN 子句)

hive>select a.id from t1 a left semi join t2 b on (a.id=b.id);

仅列出 t1 中仅存在于 t1 但不存在于 t2 的 id(基本上是 NOT IN 子句)

hive>select a.id from t1 a left outer join t2 b on(a.id=b.id) where b.id is null;

assume table t1(id,name) and table t2(id,name)

listing only those ids from t1 that exists in t2(basically IN clause)

hive>select a.id from t1 a left semi join t2 b on (a.id=b.id);

listing only those ids from t1 that exists only in t1 but not in t2(basically NOT IN clause)

hive>select a.id from t1 a left outer join t2 b on(a.id=b.id) where b.id is null;
泼猴你往哪里跑 2024-12-15 10:10:53

从 Hive 0.13 开始,Hive 确实支持 IN/EXISTS 语句,但有一些限制。请参考 https://cwiki.apache.org/confluence/display/Hive /LanguageManual+SubQueries 了解更多详细信息。

Hive does support IN/EXISTS statements since Hive 0.13 with few limitations. Please refer to https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SubQueries for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文