首页 > 解决方案 > aws athena 转换数组> 到餐桌

问题描述

我有一个 s3 存储桶,其中包含很多包含以下内容的文件:

{time:123456, state:{{1,2,3,4},{4,5,6,7}...}

在我使用 Athena 读取它之后,结果是具有 2 个列的数据集:第一个是 time int,第二个是array<array<string>>,是否可以使用 athena sql 将此表转换为:

select time, col1,col2,col3,col4
from table

当 col1...4 是数组中列的名称时?

标签: sqlarraysamazon-s3type-conversionamazon-athena

解决方案


使用CROSS JOIN UNNEST 取消嵌套上层数组:

select a.time, 
       state_array[1] as col1, 
       state_array[2] as col2,
       state_array[3] as col3,
       state_array[4] as col4
from my_table a
CROSS JOIN UNNEST(state) as t(state_array)

推荐阅读