AWS Athena 解析嵌套 JSON 数组

问题描述 投票:0回答:1

我有这样结构的 JSON 文件,我需要使用 AWS Athena 查询此 JSON 以提取

myarray
数组中的某些值。

最终结果应为

userIdentifier
enabled
值仅在标题 =
zz

的情况下

在这种情况下,那就是

useridentifier, enabled 
xyz-123, true

json

{
   "userIdentifier":"xyz-123",
   "myarray":[
      {
         "code":"split",
         "requiresinput":false,
         "categorycode":"strictly_necessary",
         "originalrequiresinput":false,
         "title":"xx",
         "enabled":false
      },
      {
         "code":"zz",
         "requiresinput":false,
         "categorycode":"zz",
         "originalrequiresinput":false,
         "title":"zz",
         "enabled":true
      }
   ]
}
json amazon-athena
1个回答
0
投票

解决了这个问题,分享给其他人。

  • 设置指向您的 s3 文件夹的 Glue 表
  • 对于列
    • 设置用户标识符 - varchar (1000)
    • 对于 myArray 列 - 使用数组数据类型,然后使用字符串子类型

对于查询,使用 UNNEST 命令。

with cte1 AS
(
SELECT 
   
    useridentifier
    ,json_extract_scalar(source, '$.enabled') enabled
    ,"$path" pth
FROM "testdb"."test" 
CROSS JOIN UNNEST("testdb"."test"."myArray") AS t1 (source)
where json_extract_scalar(source, '$.title') = 'zz'
)
select 
    
    useridentifier
    ,enabled
    , pth
from cte1 c
© www.soinside.com 2019 - 2024. All rights reserved.