【问题标题】:Where condition on collect_set in hive蜂巢中collect_set的条件
【发布时间】:2021-03-24 19:05:57
【问题描述】:

有没有办法可以过滤掉 hive 的 collect_set 输出中的非连续数字? 请参见下面的示例,突出显示的行没有连续的数字,我只想要这样的输出

这个数字可能是[5,6,9][4,5,7,8,] 我想从数组中的值不连续的配置单元查询中过滤所有这样的行。

【问题讨论】:

    标签: sql arrays hive hiveql


    【解决方案1】:

    分解数组,将每个元素与下一个元素进行比较以找到不连续的元素,计算出现次数并过滤。

    演示:

    set hive.on.master=true;
    with your_data as (
        select  array (5,6,9) as myarray
        union all
        select  array (1,2,3,4,5,6) as myarray
        union all
        select  array (0) as myarray
        union all
        select  array (4,5,7,8) as myarray
        union all
        select  array (0,1) as myarray
        union all
        select  array (0,1,3) as myarray
    )
    
    select myarray--, sum(non_consecutive)
    from
    (
    select myarray,
           case when lead(value) over(partition by myarray order by pos )-value != 1 then 1 else 0 end non_consecutive
      from your_data d
           lateral view posexplode(myarray) e as pos, value
    )s
    group by myarray    
    having sum(non_consecutive)>0
    ;
    

    结果:

    myarray
    [0,1,3]
    [4,5,7,8]
    [5,6,9]
    

    如果您最初有爆炸数据,那么只需在 collect_set 之前应用相同的逻辑:计算前导值(您应该知道按什么排序),与当前值比较,然后聚合集合,同时聚合 non_consecutive 和过滤器。

    【讨论】:

    • 非常感谢您的意见
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 2015-10-08
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    相关资源
    最近更新 更多