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;
查询结果如下: