在 AWS Athena 中查询嵌套 JSON 结构

erP*_*rPe 5 presto amazon-athena

我得到了以下格式的具有嵌套结构的 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"
    ]
}
Run Code Online (Sandbox Code Playgroud)

我在 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/';
Run Code Online (Sandbox Code Playgroud)

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

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

select cnds.entries from 
filters,
UNNEST(filters.groups) AS t(grps),
UNNEST(grps.conditions) AS t(cnds)
Run Code Online (Sandbox Code Playgroud)

然而,由于这是一个复杂的数组,它让我有些头痛,什么是正确的查询方法。

任何提示表示赞赏!

谢谢R

j.b*_*ski 7

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

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'
Run Code Online (Sandbox Code Playgroud)