【问题标题】:AWS Athena json_extract query from string field returns empty values来自字符串字段的 AWS Athena json_extract 查询返回空值
【发布时间】:2018-11-27 03:18:51
【问题描述】:

我在雅典娜有一张这样结构的桌子

CREATE EXTERNAL TABLE `json_test`(
  `col0` string , 
  `col1` string , 
  `col2` string , 
  `col3` string , 
  `col4` string , 
  )
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.OpenCSVSerde' 
WITH SERDEPROPERTIES ( 
  'quoteChar'='\"', 
  'separatorChar'='\;') 

这样的Json String 存储在“col4”中:

{'email': 'test_email@test_email.com', 'name': 'Andrew', 'surname': 'Test Test'}

我正在尝试进行 json_extract 查询:

SELECT json_extract(col4 , '$.email') as email FROM "default"."json_test"

但查询返回空值。

任何帮助将不胜感激。

【问题讨论】:

  • 是否引用了 json?我只是问,因为我已经看到 opencsvserde 解析所有数据而不是吐出错误,当你从 json_test 中选择 col4 时,你得到了你所期望的。
  • Json 没有被引用。如果我从 json_test 中选择 *,我会正确获取所有行
  • 如果你从 json_test 中选择 col4,你会得到包含 json 的 col 吗?
  • 我只是问,因为我刚刚运行了类似的东西并且遇到了问题。
  • 是的,你是对的,我确实从 json_test 中选择 col4 并得到了 json 字符串

标签: json amazon-s3 hive amazon-athena presto


【解决方案1】:

JSON 需要使用双引号 (") 将值括起来。

比较:

presto> SELECT json_extract('{"email": "test_email@test_email.com", "name": "Andrew"}' , '$.email');
            _col0
-----------------------------
 "test_email@test_email.com"

和

presto> SELECT json_extract('{''email'': ''test_email@test_email.com'', ''name'': ''Andrew''}', '$.email');
 _col0
-------
 NULL

(注意:SQL varchar 文字中的'' 表示构造值中的单个',因此此处的文字与问题中的格式相同。)

如果您的字符串值是“带单引号的 JSON”,您可以尝试使用 replace(string, search, replace) → varchar 修复它

【讨论】:

    【解决方案2】:

    问题是存储的 json 字符串的单引号字符

    {'email': 'test_email@test_email.com', 'name': 'Andrew', 'surname': 'Test Test'}
    

    改为双引号

    {"email": "test_email@test_email.com", "name": "Andrew", "surname": "Test Test"}
    

    Athena Query 工作正常:

    SELECT json_extract(col4 , '$.email') as email FROM "default"."json_test"
    

    【讨论】:

    • 请将@Piotr Findeisen 的答案标记为正确,因为他是第一个为您提供答案的人
    猜你喜欢
    • 2023-02-23
    • 2013-09-14
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2019-10-08
    • 2019-08-04
    • 2014-03-31
    相关资源
    最近更新 更多