ToB企服应用市场:ToB评测及商务社交产业平台

标题: 7. Hive分析JSON字符串、JSON数组 [打印本页]

作者: 铁佛    时间: 2024-7-16 18:38
标题: 7. Hive分析JSON字符串、JSON数组
Hive分析JSON字符串

1. get_json_object


  1. -- 示例1(单层JSON)
  2. SELECT get_json_object('{
  3.                             "name": "John",
  4.                             "age": 30
  5.                          }', '$.name');
  6. -- res: "John"
  7. -- 示例2(嵌套JSON)
  8. SELECT get_json_object('{
  9.                             "person": {
  10.                                 "name": "John",
  11.                                 "age": 30,
  12.                                 "address": {
  13.                                     "street": "123 Main St",
  14.                                     "city": "New York"
  15.                                 }
  16.                             }
  17.                          }', '$.person.address.street');
  18. -- res: "123 Main St"
复制代码
范围性


2. json_tuple


  1. -- 示例1(单层JSON)
  2. select json_tuple('{
  3.                         "name": "zhangsan",
  4.                         "age": 18
  5.                     }','name','age');
  6. -- res: zhangsan        18
  7. -- 示例2(嵌套JSON)
  8. SELECT json_tuple('{
  9.                         "person": {
  10.                             "name": "Alice",
  11.                             "age": 25,
  12.                             "address": {
  13.                                 "city": "New York",
  14.                                 "country": "USA"
  15.                             }
  16.                         }
  17.                     }', 'person.name', 'person.age', 'person.address.city');
  18. -- res: Alice    25    New York
复制代码
Hive分析JSON数组

前置知识

explode函数


  1. -- 解析 array
  2. hive> select explode(array('A','B','C'));
  3. OK
  4. A
  5. B
  6. C
  7. -- 解析map
  8. hive> select explode(map('A',10,'B',20,'C',30));
  9. OK
  10. A       10
  11. B       20
  12. C       30
复制代码
regexp_replace函数


  1. hive> select regexp_replace('foobar', 'oo|ar', '');
  2. OK
  3. fb
复制代码
1. 嵌套子查询分析JSON数组(利用explode+regexp_replace)

数据如下所示:
array(json_str)[{“website”:“baidu.com”,“name”:“百度”},{“website”:“google.com”,“name”:“谷歌”}] 分析出其中的website、name
websitenamebaidu.com百度google.com谷歌
  1. -- 思路
  2. -- 1. 使用 regexp_replace 函数将原数据转换为 {"website":"baidu.com","name":"百度"};{"website":"google.com","name":"谷歌"}
  3. -- 2. 使用 split 函数按照 ';' 分割 {"website":"baidu.com","name":"百度"};{"website":"google.com","name":"谷歌"},返回 [{"website":"baidu.com","name":"百度"},{"website":"google.com","name":"谷歌"}]
  4. -- 3. 使用 explode 炸裂为
  5. -- {"website":"baidu.com","name":"百度"}
  6. -- {"website":"google.com","name":"谷歌"}
  7. -- 4. 使用 json_tuple 解析数据
  8. -- 实现
  9. -- 1. 先将json数组中的元素解析出来,转化为每行显示
  10. SELECT explode(split(regexp_replace(regexp_replace(
  11.                                             '[{"website":"baidu.com","name":"百度"},{"website":"google.com","name":"谷歌"}]',
  12.                                             '\\[|\\]', ''), '\\}\\,\\{', '\\}\\;\\{'), '\\;'));
  13. -- 2. 使用 json_tuple 解析数据
  14. select json_tuple(json, 'website', 'name')
  15. from (
  16. select explode(split(regexp_replace(regexp_replace('[{"website":"baidu.com","name":"百度"},{"website":"google.com","name":"谷歌"}]', '\\[|\\]',''),'\\}\\,\\{','\\}\\;\\{'),'\\;'))
  17. as json) t1;
复制代码
上面 regexp_replace 函数中的内容分析:
  1. SELECT explode(split(
  2.     regexp_replace(
  3.         regexp_replace(
  4.             '[
  5.                 {"website":"baidu.com","name":"百度"},
  6.                 {"website":"google.com","name":"谷歌"}
  7.             ]',
  8.             '\\[|\\]' , ''), --将json数组两边的中括号去掉
  9.             
  10.               '\\}\\,\\{' , '\\}\\;\\{'), --将json数组元素之间的逗号换成分号
  11.                
  12.                  '\\;') --以分号作为分隔符(split函数以分号作为分隔)
  13.           );
  14.          
  15. -- 问:为什么要将json数组中元素之间的逗号换成分号?
  16. -- 答:因为元素内的分隔也是逗号,如果不将元素之间的逗号换掉的话,后面用split函数分隔时也会把元素内的数据给分隔,这不是我们想要的结果。
复制代码
2. 利用 lateral view 分析JSON数组

当我们数据的布局如下,
idsnames[1,2,3][{“name”: “daming”, “age”: “15”}, {“name”: “lingling”, “age”: “14”}, {“name”: “tom”, “age”: “17”}] 我们想要获取的数据为,
idname1daming2daming3daming1lingling2lingling3lingling1tom2tom3tom
  1. with json_data as (
  2.     select `array`(1, 2, 3)                        as ids,
  3.            `array`('{"name": "daming", "age": "15"}', '{"name": "lingling", "age": "14"}',
  4.                    '{"name": "tom", "age": "17"}') as json_infos
  5. )
  6. -- 使用json_tuple()报错: org.apache.hadoop.hive.ql.parse.SemanticException:
  7. -- UDTF's are not supported outside the SELECT clause, nor nested in expressions
  8. -- 原因: 未知
  9. -- SELECT id, json_tuple(json_info, 'name')
  10. SELECT id, get_json_object(json_info, '$.name')
  11. FROM json_data
  12.          lateral view explode(json_data.ids) tmp_ids as id
  13.          lateral view explode(json_data.json_infos) tmp_json_infos as json_info;
复制代码
学习链接

文心一言
ChatGPT
Hive分析Json数组超全解说

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4