如何在 Amazon Athena 上查询(搜索)具有 JSON 值的 sql?

Posted

技术标签:

【中文标题】如何在 Amazon Athena 上查询(搜索)具有 JSON 值的 sql?【英文标题】:how to query(search) of sql on Amazon Athena which has JSON value? 【发布时间】:2019-08-06 04:32:00 【问题描述】:

[查询时出现第一个错误][1]

select * from table where properties 'year' = 2007 // is not working
//please check the screen shot of the table

我想通过 sql 查询查询 Athena 数据集。我尝试了每个查询,但它不适用于此 Athena 数据

【问题讨论】:

使用json_extract。见:6.12. JSON Functions and Operators — Presto Documentation 它不工作! 请编辑您的问题以显示您尝试了什么以及发生了什么。 查看屏幕截图 【参考方案1】:

我假设properties 列是STRING,在这种情况下,您可以这样做来提取year 字段并在过滤器中使用它:

SELECT * FROM table WHERE JSON_EXTRACT_SCALAR(properties, '$.year') = '2007'

请注意,它是 '2007',而不是 2007,因为从您的屏幕截图来看,这些值看起来像是字符串。

您可以阅读有关JSON_EXTRACT_SCALAR function and other JSON manipulation functions in the documentation for the Presto version that Athena is currently using 的更多信息。

【讨论】:

以上是关于如何在 Amazon Athena 上查询(搜索)具有 JSON 值的 sql?的主要内容,如果未能解决你的问题,请参考以下文章