将json格式的数据文件存入hive中的方法

第一种

如果数据格式是Json格式,如果按照常规的思路我们要使用自定义函数去解析, 那么在这里使用JSonSerde就可以直接读取Json格式的文件,不需要做转化。

如果是第三方jar包或者是自己写的,就必须要先加载jar包:
hive> add jar /hivedata/json-serde-1.3.8-jar-with-dependencies.jar;

第二种

json格式数据表需要通过serde机制处理
1 在hive-site.xml中设置三方jar包

<property>  
	<name>hive.aux.jars.path</name>
	<value>/usr/local/hive-2.1.1/lib/</value>
</property>

2 在hive.aux.jars.path设置的路径中增加hive-hcatalog-core-2.1.1.jar 拷贝路径为$HIVE_HOME/hcatalog/share/hcatalog/hive-hcatalog-core-2.1.1.jar到/usr/local/hive-2.1.1/lib/中,重启hive即可

简单案例演示:

#数据内容:
{"pid":1,"content":"this is pid of 1 content"}
{"pid":2,"content":"this is pid of 2 content"}

#创建表
create table if not exists js1(
pid int,
content string
)
row format serde "org.openx.data.jsonserde.JsonSerDe";

#导入数据
load data local inpath '/opt/data/js.json' into table js1;

复杂案例演示

#数据内容
{"uid":1,"uname":"zs","belong":["zs1","zs2","zs3"],"tax":{"shebao":
[220,280,300],"gongjijin":[600,1200,2400]}}
{"uid":2,"uname":"ls","belong":["ls1","ls2","ls3"],"tax":{"shebao":
[260,300,360],"gongjijin":[800,1600,2600]}}

#创建表
create table if not exists complex(
uid int,
uname string,
belong array<String>,
tax map<String,array<double>>
)
row format serde "org.openx.data.jsonserde.JsonSerDe";

#插入表格
load data local inpath '/opt/data/complex.json' into table complex;

#查询
select c.*
from complex c
where size(c.belong) = 3 and c.tax["gongjijin"][1] > 1200;
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值