码迷,mamicode.com
首页 > 编程语言 > 详细

Hive数组转成多行

时间:2020-12-29 12:03:02      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:sql   src   split   多行   regexp   数据   col   json   get   

原始数据如下:

技术图片

 

 转换后效果:

技术图片

 

 

实现方式:

-- 原数据

SELECT ‘[{"slot_seq":"1","status":"0","battery_sn":"K212EV14C3"},{"slot_seq":"10","status":"0","battery_sn":"K482GE13RT"},{"slot_seq":"3","status":"0","battery_sn":"E513GJ11XT"},{"slot_seq":"4","status":"0","battery_sn":"K377FH1BSK"},{"slot_seq":"5","status":"0","battery_sn":"E046EH142Y"},{"slot_seq":"7","status":"0","battery_sn":"L154GVA0CP"}]‘ AS json_column
-- hive sql  实现

select  get_json_object(j_column,‘$.slot_seq‘) as slot_seq,
get_json_object(j_column,‘$.status‘) as status,
get_json_object(j_column,‘$.battery_sn‘) as battery_sn
from

(SELECT split(regexp_replace(regexp_extract(t.json_column, ‘(\\[)(.*?)(\\])‘, 2), ‘\\},\\{‘, ‘\\}|\\{‘), ‘\\|‘) AS json_column
FROM (
	SELECT ‘[{"slot_seq":"1","status":"0","battery_sn":"K212EV14C3"},{"slot_seq":"10","status":"0","battery_sn":"K482GE13RT"},{"slot_seq":"3","status":"0","battery_sn":"E513GJ11XT"},{"slot_seq":"4","status":"0","battery_sn":"K377FH1BSK"},{"slot_seq":"5","status":"0","battery_sn":"E046EH142Y"},{"slot_seq":"7","status":"0","battery_sn":"L154GVA0CP"}]‘ AS json_column
) t
) a
lateral view explode(json_column) b as j_column

  

Hive数组转成多行

标签:sql   src   split   多行   regexp   数据   col   json   get   

原文地址:https://www.cnblogs.com/kopao/p/14182859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!