字符串字段中的AWS Athena json_extract查询返回空值 [英] AWS Athena json_extract query from string field returns empty values

查看:489
本文介绍了字符串字段中的AWS Athena json_extract查询返回空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a table in athena with this structure

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"中:

A Json String like this is stored in "col4":

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

我正在尝试进行json_extract查询:

I´m trying to make a json_extract query:

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

但是查询返回空值.

任何帮助将不胜感激.

推荐答案

JSON需要使用双引号(")来封闭值.

The JSON needs to use double quotes (") for enclosing values.

比较:

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文字内部的''表示构造值中的单个',因此此处的文字格式与问题中的格式相同.)

(Note: '' inside SQL varchar literal mean single ' in the constructed value, so the literal here is the same format that in the question.)

如果您的字符串值是带单引号的JSON",则可以尝试使用

If your string value is a "JSON with single quotes", you can try to fix it with replace(string, search, replace) → varchar

这篇关于字符串字段中的AWS Athena json_extract查询返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆