简体   繁体   English

如何使用 Spark SQL 查询反转数组中的元素?

[英]How to reverse an elements in the array using Spark SQL query?

I have a data like below Input data Key data a [5,2,6,null,null] b [5,7,9,4,null,null,null]我有如下数据 输入数据 关键数据 a [5,2,6,null,null] b [5,7,9,4,null,null,null]

I want output to be like below.我希望 output 如下所示。 Output: Key data a [6,2,5,null,null] b [4,9,7,5,null,null,null] Output:关键数据a [6,2,5,null,null] b [4,9,7,5,null,null,null]

Basically elements in the array needs to be reversed by keeping nulls at the end as it is.基本上数组中的元素需要通过在末尾保留空值来反转。 Can someone please help me with spark SQL query?有人可以帮我查询 spark SQL 吗?

My approach - transform NULLs for sorting then transform back to NULL我的方法 - 转换 NULL 进行排序然后转换回 NULL

select transform(sort_array(transform(data, x -> coalesce(x, 0)), False), x -> case when x=0 then null else x end) from table1

[EDIT] [编辑]

Just noticed the transformations are not required if the NULLs are to be sorted at the end based on reverse order.刚刚注意到,如果要在末尾基于倒序对 NULL 进行排序,则不需要进行转换。 sort_array() will work by itself sort_array()将自行工作

sort_array(data, False)

[EDIT 2] [编辑 2]

Having it pointed out to me I misunderstood the question, I believe this will work... it's a little convoluted however:它向我指出我误解了这个问题,我相信这会起作用......但是它有点令人费解:

select 
 concat( 
    reverse(array_except(array(5,7,9,4,null,null,null), array(null))) 
  , array_repeat(null
    , aggregate(
        transform(array(5,7,9,4,null,null,null), (x, i) -> (case when x is null then 1 else 0 end))
        , 0, (acc, x) -> acc + x
       )
     )
 )

The approach counts the number of nulls, removes the nulls, reverses the array and adds back the nulls at the end of the array该方法计算空值的数量,删除空值,反转数组并在数组末尾添加空值

Untested:未经测试:

reverse(filter(array(0, null, 2, 3, null), x -> x IS NOT NULL))

then append:然后是 append:

filter(array(0, null, 2, 3, null), x -> x IS NULL)

See Filter查看 过滤器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM