hive默认使用分隔符如空格,分号,"|",制表符\t来格式化数据记录,对于复杂数据类型如json,nginx日志等,就没有办法拆分了,这时候需要更加强大的SerDe来处理复杂数据,如使用JsonSerDe或者使用正则表达式RegSerDe来处理。这里介绍如何利用JsonSerDe来处理json数据。
1、下载最新的json-serde-1.3.8-jar-with-dependencies.jar和json-udf-1.3.8-jar-with-dependencies.jar到hive中的lib目录下。
2、创建表。
hive> create table employees(name string,salary int) comment "a json format table" row format serde 'org.openx.data.jsonserde.JsonSerDe' stored as textfile; OK Time taken: 3.951 seconds hive> show tables; OK employees Time taken: 0.14 seconds, Fetched: 1 row(s)
3、加载数据并测试。
hive> load data local inpath 'file:/home/hadoop/spark/examples/src/main/resources/employees.json' overwrite into table employees; Loading data to table default.employees OK Time taken: 2.153 seconds hive> select * from employees; OK Michael 3000 Andy 4500 Justin 3500 Berta 4000 Time taken: 1.467 seconds, Fetched: 4 row(s)