aws athena json_extract query from string字段返回空值

8mmmxcuj  于 2021-06-26  发布在  Hive
关注(0)|答案(2)|浏览(372)

我在雅典娜有一张这样的table

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提取查询:

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

但是查询返回空值。
任何帮助都将不胜感激。

hc8w905p

hc8w905p1#

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 literal mean single ' 在构造的值中,因此这里的文本与问题中的格式相同。)
如果您的字符串值是“带单引号的json”,您可以尝试用 replace(string, search, replace) → varchar

vngu2lb8

vngu2lb82#

问题是存储的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"

相关问题