hive json数据生成和处理
2016-03-24 16:14
2911 查看
场景:查询结果封装成json格式
实现:封装记录为json格式可以编写自定义函数,也可以直接使用concat函数直接拼接,下面直接使用concat函数拼接实现将数据行转化为json数据
select concat('{\"id\":\"',
t.id,
'\",\"index_date\":\"',
NVL(t.index_date, ''),
'\",\"index_name\":\"',
NVL(t.index_name, ''),
'\",\"index_value\":\"',
NVL(t.index_value, ''),
'\"}') as value
from tbl_test t
注意:concat函数在连接元素时,需要注意如某一个元素为空时,concat函数直接返回null,所以需要对null元素做特殊处理,这里是将null转为空字符串。
hive 有直接解析 json 数据的函数 get_json_object(stringjson_string, string path)
将上述json数据封装完成后,我们存入表 tbl_test_json,插入字段 json_line 中
使用get_json_object函数解析该json数据:
select get_json_object(t.json_line, '$.id'),
get_json_object(t.json_line, '$.index_date'),
get_json_object(t.json_line, '$.index_name'),
get_json_object(t.json_line, '$.index_value')
from tbl_test_json t;
PS:get_json_object使用说明:
A limited version of JSONPath is supported:
$ : Root object
. : Child operator
[] : Subscript operator for array
* : Wildcard for []
Syntax not supported that's worth noticing:
: Zero length string as key
.. : Recursive descent
@ : Current object/element
() : Script expression
?() : Filter (script) expression.
[,] : Union operator
[start:end.step] : array slice operator
Example: src_json table is a single column (json), single row table:
+----+ json +----+
{"store":
{"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],
"bicycle":{"price":19.95,"color":"red"}
},
"email":"amy@only_for_json_udf_test.net",
"owner":"amy"
}
+----+
The fields of the json object can be extracted using these queries:
hive> SELECT get_json_object(src_json.json, '$.owner') FROM src_json;
amy
hive> SELECT get_json_object(src_json.json, '$.store.fruit\[0]') FROM src_json;
{"weight":8,"type":"apple"}
hive> SELECT get_json_object(src_json.json, '$.non_exist_key') FROM src_json;
NULL
实现:封装记录为json格式可以编写自定义函数,也可以直接使用concat函数直接拼接,下面直接使用concat函数拼接实现将数据行转化为json数据
select concat('{\"id\":\"',
t.id,
'\",\"index_date\":\"',
NVL(t.index_date, ''),
'\",\"index_name\":\"',
NVL(t.index_name, ''),
'\",\"index_value\":\"',
NVL(t.index_value, ''),
'\"}') as value
from tbl_test t
注意:concat函数在连接元素时,需要注意如某一个元素为空时,concat函数直接返回null,所以需要对null元素做特殊处理,这里是将null转为空字符串。
hive 有直接解析 json 数据的函数 get_json_object(stringjson_string, string path)
将上述json数据封装完成后,我们存入表 tbl_test_json,插入字段 json_line 中
使用get_json_object函数解析该json数据:
select get_json_object(t.json_line, '$.id'),
get_json_object(t.json_line, '$.index_date'),
get_json_object(t.json_line, '$.index_name'),
get_json_object(t.json_line, '$.index_value')
from tbl_test_json t;
PS:get_json_object使用说明:
A limited version of JSONPath is supported:
$ : Root object
. : Child operator
[] : Subscript operator for array
* : Wildcard for []
Syntax not supported that's worth noticing:
: Zero length string as key
.. : Recursive descent
@ : Current object/element
() : Script expression
?() : Filter (script) expression.
[,] : Union operator
[start:end.step] : array slice operator
Example: src_json table is a single column (json), single row table:
+----+ json +----+
{"store":
{"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],
"bicycle":{"price":19.95,"color":"red"}
},
"email":"amy@only_for_json_udf_test.net",
"owner":"amy"
}
+----+
The fields of the json object can be extracted using these queries:
hive> SELECT get_json_object(src_json.json, '$.owner') FROM src_json;
amy
hive> SELECT get_json_object(src_json.json, '$.store.fruit\[0]') FROM src_json;
{"weight":8,"type":"apple"}
hive> SELECT get_json_object(src_json.json, '$.non_exist_key') FROM src_json;
NULL
相关文章推荐
- 分享Hive的一份胶片资料
- Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别
- XML 与 JSON 优劣对比
- VBA将excel数据表生成JSON文件
- newtonsoft.json解析天气数据出错解决方法
- vbs 解析json jsonp的方法
- Extjs4如何处理后台json数据中日期和时间
- C#实现将类的内容写成JSON格式字符串的方法
- JQuery ajax返回JSON时的处理方式 (三种方式)
- jquery JSON的解析方式示例介绍
- c#版json数据解析示例分享
- ASP JSON类文件的使用方法
- C#实现对Json字符串处理实例
- C#实现Json转Unicode的方法
- php解析json数据实例
- PHP中JSON的应用技巧
- PHP6 先修班 JSON实例代码
- 关于JSON以及JSON在PHP中的应用技巧
- .Net中的json操作类用法分析
- C#操作 JSON方法汇总