【问题标题】:Hive SQL, querying a column of arrays using where statementHive SQL,使用 where 语句查询一列数组
【发布时间】:2019-04-21 16:32:38
【问题描述】:

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

例如,我有一列用户名和用户类型,那么每个用户名可能有多个类型

所以当我使用

 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 and usertype[1] = 4”等等。有人对此事有任何建议或想法吗?

谢谢

【问题讨论】:

    标签: hive hiveql


    【解决方案1】:

    您无法使用= 比较非原始类型。

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

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

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

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

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 2014-04-12
    • 2014-05-26
    • 2012-11-26
    • 2014-10-13
    • 1970-01-01
    • 2017-05-20
    相关资源
    最近更新 更多