繁体   English   中英

AWS Athena - 查询 JSON - 搜索值

[英]AWS Athena - Querying JSON - Searching for Values

我在 S3 上嵌套了 JSON 文件,并尝试使用 Athena 查询它们。

但是,我在查询嵌套的 JSON 值时遇到问题。

我的 JSON 文件如下所示:

 {
  "id": "17842007980192959",
  "acount_id": "17841401243773780",
  "stats": [
    {
      "name": "engagement",
      "period": "lifetime",
      "values": [
        {
          "value": 374
        }
      ],
      "title": "Engagement",
      "description": "Total number of likes and comments on the media object",
      "id": "17842007980192959/insights/engagement/lifetime"
    },
    {
      "name": "impressions",
      "period": "lifetime",
      "values": [
        {
          "value": 11125
        }
      ],
      "title": "Impressions",
      "description": "Total number of times the media object has been seen",
      "id": "17842007980192959/insights/impressions/lifetime"
    },
    {
      "name": "reach",
      "period": "lifetime",
      "values": [
        {
          "value": 8223
        }
      ],
      "title": "Reach",
      "description": "Total number of unique accounts that have seen the media object",
      "id": "17842007980192959/insights/reach/lifetime"
    },
    {
      "name": "saved",
      "period": "lifetime",
      "values": [
        {
          "value": 0
        }
      ],
      "title": "Saved",
      "description": "Total number of unique accounts that have saved the media object",
      "id": "17842007980192959/insights/saved/lifetime"
    }
  ],
  "import_date": "2017-12-04"
}

我想要做的是查询 name=impressions 的“stats”字段值。

所以理想情况下是这样的:

SELECT id, account_id, stats.values.value WHERE stats.name='engagement'

AWS 示例: https : //docs.aws.amazon.com/athena/latest/ug/searching-for-values.html

任何帮助将不胜感激。

您可以使用以下表定义查询 JSON:

CREATE EXTERNAL TABLE test(
id string,
acount_id string,
stats array<
  struct<
     name:string,
     period:string,
     values:array<
         struct<value:string>>,
     title:string
  >
 >
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
LOCATION 's3://bucket/';

现在, value列可通过以下取消嵌套使用:

select id, acount_id, stat.name,x.value
from test
cross join UNNEST(test.stats) as st(stat)
cross join UNNEST(stat."values") as valx(x)
WHERE stat.name='engagement';

暂无
暂无

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

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