来自字符串字段的AWS Athena json_extract查询返回空值

时间:2018-06-18 09:48:32

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

我在athena有一个带有这种结构的表

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字符串存储在“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"

但查询返回空值。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:3)

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修复它

答案 1 :(得分:0)

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

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

更改为双引号

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

雅典娜查询正常工作:

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