其他分享
首页 > 其他分享> > Hiv 语法

Hiv 语法

作者:互联网

HIV语法

a:shangdong,b:beijing,c:shanghai	1,2,3	[{"source":"7fresh","monthSales":4900,"userCount":1900,"score":"9.9"},{"source":"jdmart","monthSales":7900,"userCount":2900,"score":"5.9"},{"source":"yam","monthSales":54900,"userCount":12900,"score":"4.9"}]
a:tianjing,b:beijing,c:shanghai	3,4,5	[{"source":"7fresh","monthSales":4900,"userCount":1900,"score":"9.9"},{"source":"jdmart","monthSales":7900,"userCount":2900,"score":"5.9"},{"source":"yam","monthSales":54900,"userCount":12900,"score":"4.9"}]

#去掉开头和结尾的[{、}]
select regexp_replace(sale_info,'\\[\\{|\\}\\]','') from explode_test

"source":"7fresh","monthSales":4900,"userCount":1900,"score":"9.9"},{"source":"jdmart","monthSales":7900,"userCount":2900,"score":"5.9"},{"source":"yam","monthSales":54900,"userCount":12900,"score":"4.9"
"source":"7fresh","monthSales":4900,"userCount":1900,"score":"9.9"},{"source":"jdmart","monthSales":7900,"userCount":2900,"score":"5.9"},{"source":"yam","monthSales":54900,"userCount":12900,"score":"4.9"

# 拆分并转成多行
select explode(split(regexp_replace(sale_info,'\\[\\{|\\}\\]',''),"\\},\\{")) from explode_test
~~~显示结果
"source":"7fresh","monthSales":4900,"userCount":1900,"score":"9.9"
"source":"jdmart","monthSales":7900,"userCount":2900,"score":"5.9"
"source":"yam","monthSales":54900,"userCount":12900,"score":"4.9"
"source":"7fresh","monthSales":4900,"userCount":1900,"score":"9.9"
"source":"jdmart","monthSales":7900,"userCount":2900,"score":"5.9"
"source":"yam","monthSales":54900,"userCount":12900,"score":"4.9"

通过get_json_object函数转换成json并获取json中属性

select
get_json_object(concat("{",t.infos,"}"),"$.source"),
get_json_object(concat("{",t.infos,"}"),"$.monthSales"),
get_json_object(concat("{",t.infos,"}"),"$.score")
from
(
select explode(split(regexp_replace(sale_info,'\[\{|\}\]',''),"\},\{")) as infos from explode_test
)t

7fresh	4900	9.9
jdmart	7900	5.9
yam	54900	4.9
7fresh	4900	9.9
jdmart	7900	5.9
yam	54900	4.9

扩展,如果显示area,如何处理,可以通过udtf即lateral view进行实现

select
area,
get_json_object(concat("{",infos,"}"),"$.source"),
get_json_object(concat("{",infos,"}"),"$.monthSales"),
get_json_object(concat("{",infos,"}"),"$.score")
from explode_test lateral view explode(split(regexp_replace(sale_info,'\[\{|\}\]',''),"\},\{")) g as infos



针对hive一些配置说明
1、当select * from xxx,不能显示列名的时候,可以到hive-site.xml里面添加
   <property>
       <name>hive.cli.print.current.db</name>
       <value>true</value>
       </property>
   <property>
       <name>hive.cli.print.header</name>
       <value>true</value>
   </property>
或者进入hive之后 
   – set hive.cli.print.current.db=true;
   – set hive.cli.print.header=true;

标签:monthSales,string,Hiv,语法,source,score,select,userCount
来源: https://www.cnblogs.com/sjkzy/p/15092253.html