hive存储读取json格式的数据

1. 创建表

CREATE TABLE tmp_json_test (
           appkey string, 
           json string
) 
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '|' 
STORED AS textfile ;

2. 导入数据,样例如下:

 load data local inpath '/home/jb-gongmingfeng/test_data.log' overwrite into table tmp_json_test;

appkey001|{"count":2,"usage":91273,"pkg":"com.example.gotest"}
appkey001|{"count":234,"usage":9876,"pkg":"com.example.gotest"}
appkey001|{"count":34,"usage":5432,"pkg":"com.example.msg"}


3. 读取json的数据有两种方法。

   方法一:

select t.appkey , get_json_object(t.json,'$.count'), get_json_object(t.json,'$.usage') from tmp_json_test t ;


    方法二:

select t1.appkey, t2.* from tmp_json_test t1 lateral view json_tuple(t1.json, 'count', 'usage') t2 as c1, c2;

查询结果相同,如下:

appkey001	2	91273
appkey001	234	9876
appkey001	34	5432
appkey001	56	3454
appkey001	354	3557
appkey001	12	79090
appkey001	5	2145
appkey001	3	5673
appkey001	75	3457
appkey001	2	6879


4. 总结一下,方法一使用函数get_json_object  , 方法二使用函数 json_tuple 。






你可能感兴趣的:(hive)