如何从Hive中的json字符串中提取选定的值

jim*_*on 6 sql json hadoop hive hiveql

我在Hive中运行一个简单的查询,产生以下输出(还有一些其他的列.

|------|-----------------------------------------------------------|
| col1 | col2                                                      |
|------|-----------------------------------------------------------|
|   A  | {"variable1":123,"variable2":456,"variable3":789}         |                                          
|------|-----------------------------------------------------------|
|   B  | {"variable1":222,"variable2":333,"variable3":444}         |
--------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

我需要能够解析json字符串并在SELECT语句本身期间拉出每个标记的值,以便我可以合并一个WHERE语句来仅返回对我有价值的字符串部分.

所以我的最终输出可能如下所示:

|------------------------------------------|
| col1 |variable1 | variable2 | variable3  |                                      
|------------------------------------------|
|  A   |   123    |    456    |    789     |                                    
|------------------------------------------|
|  B   |   222    |    333    |    444     |
--------------------------------------------
Run Code Online (Sandbox Code Playgroud)

我尝试使用各种函数包括SPLIT和GET_JSON_OBJECT使用esnap中指定的参数结构,但所有返回错误,例如:

No matching method for class org.apache.hadoop.hive.ql.udf.UDFJson 
with (struct<...>, string). Possible choices: _FUNC_(string, string)
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我,我想要做的是可行的,还是解释我哪里出错了?

提前致谢

Pra*_*ala 9

select col1, get_json_object(col2,'$.variable1') as variable1,
get_json_object(col2,'$.variable2') as variable2,
get_json_object(col2,'$.variable3') as variable3 
from json_test
Run Code Online (Sandbox Code Playgroud)

如果将输出放入表(例如json_test),则可以用这种方式解析.您也可以调整查询以获得这些结果.

输出:

col1 |variable1 |variable2 |variable3 |
-----|----------|----------|----------|
A    |123       |456       |789       |
B    |222       |333       |444       |
Run Code Online (Sandbox Code Playgroud)