Hive检查数组中的元素 [英] Hive check elements in array

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

问题描述

我有一个由user_id和item_id(由用户购买的项目的id)组成的Hive表。
我想获得所有购买商品1但不包含商品2和3的用户列表。

为此,我写了一个简单的查询:

/ p>

pre $ SELECT user_id,collect_set(item_id)itemslist FROM mytable
WHERE item_id in(1,2)
GROUP BY user_id
HAVING - 我应该在这里放什么?

正如您所看到的,我不知道如何检查数组itemslist是否包含1 2。

你如何做到这一点?
如果有一些更高效的方法可以请告诉我两个(或更多)方法吗?

解决方案

Hive中的一些集合函数(参见集合函数: https://cwiki.apache .org / confluence / display / Hive / LanguageManual + UDF )可以在这里使用。

您可以使用 array_contains(Array< T>,value)函数检查项目1是否存在, size(Array< T>)函数确保长度是1.如果两个条件都满足,你将得到所需的输出。 b

I have an Hive table made of user_id and item_id (id of items that have been purchased by the user). I want to get a list of all the users who purchased item 1 but not item 2 and 3.

To do this I wrote the simple query:

SELECT user_id, collect_set(item_id) itemslist FROM mytable
WHERE item_id in (1, 2)
GROUP BY user_id
HAVING -- what should I put here???

As you can see, I don't know how to check whether the array itemslist contains 1 and not 2.

How do you do this? If there is some more efficient way can you please tell me both (or more) methods?

解决方案

There are some collection functions in Hive `(See collection functions here : https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF ) which can use here.

You can use the array_contains(Array<T>, value) function to check if item 1 is present and the size(Array<T>) function to make sure the length is 1. If both conditions are satisfied, you will get the desired output.

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

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