【问题标题】:Querying nested JSON structures in AWS Athena在 AWS Athena 中查询嵌套的 JSON 结构
【发布时间】:2019-04-18 06:45:29
【问题描述】:

我得到了以下格式的带有嵌套结构的 JSON 文档

{
    "id": "p-1234-2132321-213213213-12312",
    "name": "athena to the rescue",
    "groups": [
        {
            "strategy_group": "anyOf",
            "conditions": [
                {
                    "strategy_conditions": "anyOf",
                    "entries": [
                        {
                            "c_key": "service",
                            "C_operation": "isOneOf",
                            "C_value": "mambo,bambo,jumbo"
                        },
                        {
                            "c_key": "hostname",
                            "C_operation": "is",
                            "C_value": "lols"
                        }
                    ]
                }
            ]
        }
    ],
    "tags": [
        "aaa",
        "bbb",
        "ccc"
    ]
}

我在 Athena 中创建了表来支持它使用以下

CREATE EXTERNAL TABLE IF NOT EXISTS filters ( id string, name string, tags array<string>, groups array<struct<
    strategy_group:string,
    conditions:array<struct<
        strategy_conditions:string,
        entries: array<struct<
            c_key:string,
            c_operation:string,
            c_value:string
        >>
    >>
>> ) row format serde 'org.openx.data.jsonserde.JsonSerDe' location 's3://filterios/policies/';

我目前的目标是也根据条件条目列进行查询。我尝试了一些查询,但是 sql 语言不是我最大的交易;)

我现在得到了这个查询,它给了我条目

select cnds.entries from 
filters,
UNNEST(filters.groups) AS t(grps),
UNNEST(grps.conditions) AS t(cnds)

但是,由于这是一个复杂的数组,它让我有些头疼,什么是正确的查询方式。

任何提示表示赞赏!

谢谢 回复

【问题讨论】:

    标签: amazon-athena presto


    【解决方案1】:

    我不确定我是否很好地理解了您的查询。看看下面这个例子,也许对你有用。

    select id, name, tags,
    grps.strategy_group,
    cnds.strategy_conditions,
    enes.c_key,enes.c_operation, enes.c_value from 
    filters,
    UNNEST(filters.groups) AS t(grps),
    UNNEST(grps.conditions) AS t(cnds),
    UNNEST(cnds.entries) AS t(enes)
    where enes.c_key='service'
    

    【讨论】:

      【解决方案2】:

      这是我最近使用的一个示例,可能会有所帮助:

      我的 JSON:

      {
      "type": "FeatureCollection",
      "features": [{
          "first": "raj",
          "geometry": {
              "type": "Point",
              "coordinates": [-117.06861096, 32.57889962]
          },
          "properties": "someprop"
      }] 
      }
      

      创建外部表:

      CREATE EXTERNAL TABLE `jsondata`(
        `type` string COMMENT 'from deserializer', 
        `features` array<struct<type:string,geometry:struct<type:string,coordinates:array<string>>>> COMMENT 'from deserializer')
      ROW FORMAT SERDE 
        'org.openx.data.jsonserde.JsonSerDe' 
      WITH SERDEPROPERTIES ( 
        'paths'='features,type') 
      STORED AS INPUTFORMAT 
        'org.apache.hadoop.mapred.TextInputFormat' 
      OUTPUTFORMAT 
        'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
      LOCATION
        's3://vicinitycheck/rawData/jsondata/'
      TBLPROPERTIES (
        'classification'='json')
      

      查询数据:

      SELECT type AS TypeEvent,
           features[1].geometry.coordinates AS FeatherType
      FROM test_vicinitycheck.jsondata
      WHERE type = 'FeatureCollection'
      

      test_vicinitycheck - 是我在 Athena 中的数据库名称
      jsondata - Athena 中的表名

      如果有帮助,我会在我的博客上记录一些示例: http://weavetoconnect.com/aws-athena-and-nested-json/

      【讨论】:

        猜你喜欢
        • 2022-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-01
        • 2020-01-13
        • 1970-01-01
        • 2020-09-21
        • 1970-01-01
        相关资源
        最近更新 更多