简体   繁体   English

从 AWS Athena 或 Presto 中的 Json 中提取

[英]Extracting from Json in AWS Athena or Presto

My query below does not give me any result我下面的查询没有给我任何结果

 WITH dataset AS (
    SELECT responseelements FROM cloudtrail_logs 
    WHERE useridentity.type = 'Root'
    AND eventname='CreateVpc'
    ORDER BY eventsource, eventname;
        AS blob
    )
    SELECT
      json_extract(blob, '$.vpc.vpcId') AS name,
      json_extract(blob, '$.ownerId') AS projects
    FROM dataset

But if I run only the inner query但是如果我只运行内部查询

SELECT responseelements FROM cloudtrail_logs 
WHERE useridentity.type = 'Root'
AND eventname='CreateVpc'
ORDER BY eventsource, eventname;

it gives me the correct response as a Json它给了我作为 Json 的正确响应

{"requestId":"40aaffac-2c53-419e-a678-926decc48557","vpc":{"vpcId":"vpc-01eff2919c7c1da07","state":"pending","ownerId":"347612567792","cidrBlock":"10.0.0.0/26","cidrBlockAssociationSet":{"items":[{"cidrBlock":"10.0.0.0/26","associationId":"vpc-cidr-assoc-04136293a8ac73600","cidrBlockState":{"state":"associated"}}]},"ipv6CidrBlockAssociationSet":{},"dhcpOptionsId":"dopt-92df95e9","instanceTenancy":"default","tagSet":{},"isDefault":false}}

and if I pass this as data as below如果我将其作为数据传递如下

WITH dataset AS (

SELECT '{"requestId":"40aaffac-2c53-419e-a678-926decc48557","vpc":{"vpcId":"vpc-01eff2919c7c1da07","state":"pending","ownerId":"347612567792","cidrBlock":"10.0.0.0/26","cidrBlockAssociationSet":{"items":[{"cidrBlock":"10.0.0.0/26","associationId":"vpc-cidr-assoc-04136293a8ac73600","cidrBlockState":{"state":"associated"}}]},"ipv6CidrBlockAssociationSet":{},"dhcpOptionsId":"dopt-92df95e9","instanceTenancy":"default","tagSet":{},"isDefault":false}}'

    AS blob
)
SELECT
  json_extract(blob, '$.vpc.vpcId') AS name,
  json_extract(blob, '$.ownerId') AS projects
FROM dataset

it gives me result, what I am missing here?它给了我结果,我在这里缺少什么? So that I am able to make it run in one shot Is it at all possible?这样我就可以一口气运行它有可能吗?

You're referencing the wrong column name in your query, it should be json_extract(responseelements, '$.vpc.vpcId') AS name instead of json_extract(blob, '$.vpc.vpcId') AS name .您在查询中引用了错误的列名,它应该是json_extract(responseelements, '$.vpc.vpcId') AS name而不是json_extract(blob, '$.vpc.vpcId') AS name The AS blob part of this query does nothing since you can't alias an entire query, so take it out.这个查询的AS blob部分什么都不做,因为你不能为整个查询加上别名,所以把它拿出来。

The AS blob works in your last example because you're selecting a value (the json string) into a column and the AS blob gives the column a name or alias of "blob". AS blob在您的上一个示例中有效,因为您正在将一个值(json 字符串)选择到一个列中,并且AS blob为该列提供了一个名称或别名“blob”。 In your original query, you're selecting an existing column named responseelements so that's what you need to refer to in the json_extract function.在您的原始查询中,您选择了一个名为responseelements的现有列,这就是您需要在json_extract function 中引用的内容。

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

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