總體來說,有兩大類方法:
1、将json以字元串的方式整個入Hive表,然後使用LATERAL VIEW json_tuple的方法,擷取所需要的列名。
2、将json拆成各個字段,入Hive表。這将需要使用第三方的SerDe,例如:https://code.google.com/p/hive-json-serde/
本文将主要使用第二種方法。
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等語句進行操作了。