简体   繁体   English

如何在 AWS Athena 中获取数组的切片?

[英]how to get slice of an array in AWS Athena?

I have an array of unknown length in AWS Athena.我在 AWS Athena 中有一个长度未知的数组。 I want to get all elements expect for the first one and concatenate into a string.我想获取第一个元素所期望的所有元素并连接成一个字符串。

I can do with a known length, but I don't see how for unknown length.我可以使用已知长度,但我不知道如何使用未知长度。 In this example:在这个例子中:

select this_arr, second, array_join(myslice, ' ') as myslice_joined
from
(select this_arr, element_at(this_arr, 2) as second, slice(this_arr, 2, 4) as myslice
from 
(select array ['one','two','three', 'four'] as this_arr));

在此处输入图像描述

What I want is myslice_joined.我想要的是 myslice_joined。 I could use slice because I knew it had four elements, but what if it's more?我可以使用 slice 因为我知道它有四个元素,但如果它更多呢? Slice does not take a -1 as the last element, as you can do elsewhere. Slice 不像其他地方那样将 -1 作为最后一个元素。

You can use cardinality to determine the array length:您可以使用cardinality来确定数组长度:

select this_arr,
    second,
    array_join(myslice, ' ') as myslice_joined
from (
        select this_arr,
            element_at(this_arr, 2) as second,
            slice(this_arr, 2, cardinality(this_arr)) as myslice
        from (
                select array [ 'one', 'two', 'three', 'four' ] as this_arr
            )
    );

Output: Output:

this_arr this_arr second第二 myslice_joined myslice_joined
[one, two, three, four] [一二三四] two two three four二三四

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

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