您的位置:首页 > Web前端 > JavaScript

如何在Hive中使用Json格式数据

2015-12-28 08:55 666 查看
本文参考了:http://pkghosh.wordpress.com/2012/05/06/hive-plays-well-with-json/

表示感谢!

总体来说,有两大类方法:

1、将json以字符串的方式整个入Hive表,然后使用LATERAL VIEW json_tuple的方法,获取所需要的列名。

2、将json拆成各个字段,入Hive表。这将需要使用第三方的SerDe,例如:https://code.google.com/p/hive-json-serde/

本文将主要使用第二种方法。

wget https://hive-json-serde.googlecode.com/files/hive-json-serde-0.2.jar
# 添加jar包
hive> add jar /home/heyuan.lhy/develop/wanke_http_test/hive-json-serde-0.2.jar;
hive>

# 创建hive表
CREATE TABLE test_json
(
id BIGINT,
text STRING,
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.contrib.serde2.JsonSerde'
STORED AS TEXTFILE
;

LOAD DATA LOCAL INPATH "test.json" OVERWRITE INTO TABLE test_json;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

wget https://hive-json-serde.googlecode.com/files/hive-json-serde-0.2.jar
 
# 添加jar包

hive>
add jar
/home/heyuan.lhy/develop/wanke_http_test/hive-json-serde-0.2.jar;
hive>

 
# 创建hive表

CREATE TABLE test_json
(

    id BIGINT,
    text
STRING,

)
ROW
FORMAT SERDE
'org.apache.hadoop.hive.contrib.serde2.JsonSerde'

STORED AS
TEXTFILE
;

 
LOAD
DATA LOCAL INPATH
"test.json" OVERWRITE
INTO TABLE test_json;

之后,就可以使用 SELECT等语句进行操作了。

备注:这个SerDe虽然比较老,但经过测试,支持到0.12的版本无压力。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: