Hive SQL,使用where语句查询一列数组

问题描述 投票:0回答:1

我只是对使用数组列的表中的where语句过滤我的查询感到好奇

例如,我有一个username和usertype列,然后每个用户名可能有多个类型

所以我用的时候

 select username, collect_set(usertype) as type from table group by username

然后我会有类似的东西:user1,[1,2,3],user2,[3,4,5]等等。问题是当我想使用“where usertype = [3,4,5]”过滤结果时。我不知道如何构造一个数组来进行过滤,现在我一直在使用“where usertype [0] = 3和usertype [1] = 4”等等。任何人对此事有任何建议或想法?

谢谢

hive hiveql
1个回答
2
投票

您无法使用=比较非基本类型。

一种解决方案是将type转换为字符串,以便集合为array<string>,然后使用concat_ws与字符串进行比较:

select * from table where concat_ws(' ', usertype) = "1 2 3";

另一种选择是编写一个比较两个array<int>参数的自定义UDF。

我找到了几个数组UDF的实现,包括arrayEquals here

© www.soinside.com 2019 - 2024. All rights reserved.