当前位置: 移动技术网 > IT编程>数据库>其他数据库 > hive函数应用之操作json

hive函数应用之操作json

2018年09月10日  | 移动技术网IT编程  | 我要评论

1、创建表

  createtable.sql中存放的创建表语句如下

create external table adt.jsontest
(
    appkey string comment "appkey",
    clickjson string comment "测试json"
) 
partitioned by(dt string comment "按照天进行分区") 
row format delimited
fields terminated by '|' 
lines terminated by '\n';

执行如下命令

hive -f createtable.sql

2、导入数据

  数据数据文件如下

  data.txt

apds|{"name":"zhangsan","age":23}
apds|{"name":"lisi","age":24}
apds|{"name":"wangwu","age":25}
apds|{"name":"zhaoliu","age":26}

  将数据上产到hdfs

hdfs dfs -copyfromlocal data.txt /data/test/2018-09-10/

  加载外部表

  在hive命令行执行如下语句

alter table adt.jsontest add partition (dt="2018-09-10") location "/data/test/2018-09-10/";

 3、查询数据

  get_json_object()函数进行查询

select get_json_object(t.clickjson,'$.name'),get_json_object(t.clickjson,'$.age')  from adt.jsontest t

  json_tuple()函数进行查询

select t2.* from adt.jsontest t1 lateral view json_tuple(t1.clickjson, 'name', 'age') t2 as b1, b2;

  查询结果如下:

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网