如何在 AWS Athena Json 查询中使用 WHERE 子句?

How can use WHERE clause in AWS Athena Json queries?

我有一个 table 存储了一些来自 Json 对象的信息:

Table:

investment
    unit(string)
    data(string)

如果 运行 查询 SELECT * FROM "db"."investment" limit 10; 我得到以下结果:

Unit Data
CH  [{"from":"CH","when":"2021-02-16","who":"pp@gmail.com"}]
AB  [{"from":"AB","when":"2020-02-16","who":"jj@gmail.com"}]

现在,我 运行 对 Json 嵌套对象中的 return 值进行以下基本查询:

SELECT json_extract_scalar(Data, '$[0].who') email FROM "db"."investment";

我得到了以下结果:

email
jj@gmail.com
pp@gmail.com

如何使用 WHERE 子句将此查询过滤为 return 只有一个值:

我试过了,但显然它不能正常工作 SQL table 行和列:

SELECT json_extract_scalar(Data, '$[0].who') email FROM "db"."investment" WHERE email = "pp@gmail.com";

有什么帮助吗?

你的问题好像有几个错别字。

  • Date in Unit Date 应该是 Data
  • key指的是什么。也许你的意思是 Data

另请注意,athena 不区分大小写,列名会转换为小写(即使您引用它们)。

除此之外,您必须在 where 子句中使用从 json 文档中提取电子邮件的完整表达式。查询的其余部分无法访问定义的列别名。

这是一个独立的例子:

with test (unit, data) as (
values
('CH',  JSON '[{"from":"CH","when":"2021-02-16","who":"pp@gmail.com"}]'),
('AB',  JSON '[{"from":"AB","when":"2020-02-16","who":"jj@gmail.com"}]')
)
select json_extract_scalar(data, '$[0].who') email
from test
where json_extract_scalar(data, '$[0].who') = 'pp@gmail.com';

outputs: 
| email        |
+--------------+
| pp@gmail.com |