Hive SQL删除数组中的空元素

时间:2018-09-21 21:09:32

标签: sql arrays hive hiveql

我不确定如何删除Hive数组中的空元素。为了说明背景 我有一个表名tbl1,其中有六列(key_1, v_1, v_2, v_3, v_4, v_5)。当我在下面查询时:

SELECT 
        key1,
        array( nvl(v_1,""),
                nvl(v_2,""),
                nvl(v_3,""),
                nvl(v_4,""),
                nvl(v_4,""),
                nvl(v_5,"")) v_array

FROM
    tbl1;

此结果产生:

key1, v_array
1, ["a","b","c","d",""]
2, ["a","b","c","",""]
3, ["a","b","","",""]

但是我希望我的结果如下所示:

key1, v_array
1, ["a","b","c","d"]
2, ["a","b","c"]
3, ["a","b"]

1 个答案:

答案 0 :(得分:1)

您可以编写自定义UDF,但执行LATERAL VIEW爆炸可能会更容易,然后进行一次收集

SELECT key1, 
      collect( val ) as v_array
FROM 
 ( SELECT key1,
          v.val as val 
   LATERAL VIEW EXPLODE( array( v1, v2, v3, v4, v5 ) ) v as val
     FROM tbl1
   WHERE val is not null
 ) lve ;