现有数据以下:html
1 huangbo guangzhou,xianggang,shenzhen a1:30,a2:20,a3:100 beijing,112233,13522334455,500
2 xuzheng xianggang b2:50,b3:40 tianjin,223344,13644556677,600
3 wangbaoqiang beijing,zhejinag c1:200 chongqinjg,334455,15622334455,20java
建表语句python
use class; create table cdt( id int, name string, work_location array<string>, piaofang map<string,bigint>, address struct<location:string,zipcode:int,phone:string,value:int>) row format delimited fields terminated by "\t" collection items terminated by "," map keys terminated by ":" lines terminated by "\n";
导入数据正则表达式
0: jdbc:hive2://hadoop3:10000> load data local inpath "/home/hadoop/cdt.txt" into table cdt;
查询语句数据库
select * from cdt;
select name from cdt;
select work_location from cdt;
select work_location[0] from cdt;
select work_location[1] from cdt;
建表语句、导入数据同1apache
查询语句json
select piaofang from cdt;
select piaofang["a1"] from cdt;
建表语句、导入数据同1服务器
查询语句app
select address from cdt;
select address.location from cdt;
不多使用函数
参考资料:http://yugouai.iteye.com/blog/1849192
和关系型数据库同样,Hive 也提供了视图的功能,不过请注意,Hive 的视图和关系型数据库的数据仍是有很大的区别:
(1)只有逻辑视图,没有物化视图;
(2)视图只能查询,不能 Load/Insert/Update/Delete 数据;
(3)视图在建立时候,只是保存了一份元数据,当查询视图的时候,才开始执行视图对应的 那些子查询
create view view_cdt as select * from cdt;
show views; desc view_cdt;-- 查看某个具体视图的信息
select * from view_cdt;
drop view view_cdt;
具体可看http://www.cnblogs.com/qingyunzong/p/8744593.html
show functions;
desc function substr;
desc function extended substr;
当 Hive 提供的内置函数没法知足业务处理须要时,此时就能够考虑使用用户自定义函数。
UDF(user-defined function)做用于单个数据行,产生一个数据行做为输出。(数学函数,字 符串函数)
UDAF(用户定义汇集函数 User- Defined Aggregation Funcation):接收多个输入数据行,并产 生一个输出数据行。(count,max)
UDTF(表格生成函数 User-Defined Table Functions):接收一行输入,输出多行(explode)
ToLowerCase.java
import org.apache.hadoop.hive.ql.exec.UDF; public class ToLowerCase extends UDF{ // 必须是 public,而且 evaluate 方法能够重载 public String evaluate(String field) { String result = field.toLowerCase(); return result; } }
add JAR /home/hadoop/udf.jar;
0: jdbc:hive2://hadoop3:10000> create temporary function tolowercase as 'com.study.hive.udf.ToLowerCase';
0: jdbc:hive2://hadoop3:10000> select tolowercase('HELLO');
现有原始 json 数据(rating.json)以下
{"movie":"1193","rate":"5","timeStamp":"978300760","uid":"1"}
{"movie":"661","rate":"3","timeStamp":"978302109","uid":"1"}
{"movie":"914","rate":"3","timeStamp":"978301968","uid":"1"}
{"movie":"3408","rate":"4","timeStamp":"978300275","uid":"1"}
{"movie":"2355","rate":"5","timeStamp":"978824291","uid":"1"}
{"movie":"1197","rate":"3","timeStamp":"978302268","uid":"1"}
{"movie":"1287","rate":"5","timeStamp":"978302039","uid":"1"}
{"movie":"2804","rate":"5","timeStamp":"978300719","uid":"1"}
{"movie":"594","rate":"4","timeStamp":"978302268","uid":"1"}
如今须要将数据导入到 hive 仓库中,而且最终要获得这么一个结果:
该怎么作、???(提示:可用内置 get_json_object 或者自定义函数完成)
返回值: string
说明:解析json的字符串json_string,返回path指定的内容。若是输入的json字符串无效,那么返回NULL。 这个函数每次只能返回一个数据项。
0: jdbc:hive2://hadoop3:10000> select get_json_object('{"movie":"594","rate":"4","timeStamp":"978302268","uid":"1"}','$.movie');
建立json表并将数据导入进去
0: jdbc:hive2://hadoop3:10000> create table json(data string); No rows affected (0.983 seconds) 0: jdbc:hive2://hadoop3:10000> load data local inpath '/home/hadoop/json.txt' into table json; No rows affected (1.046 seconds) 0: jdbc:hive2://hadoop3:10000>
0: jdbc:hive2://hadoop3:10000> select . . . . . . . . . . . . . . .> get_json_object(data,'$.movie') as movie . . . . . . . . . . . . . . .> from json;
参数为一组键k1,k2……和JSON字符串,返回值的元组。该方法比 get_json_object
高效,由于能够在一次调用中输入多个键
0: jdbc:hive2://hadoop3:10000> select . . . . . . . . . . . . . . .> b.b_movie, . . . . . . . . . . . . . . .> b.b_rate, . . . . . . . . . . . . . . .> b.b_timeStamp, . . . . . . . . . . . . . . .> b.b_uid . . . . . . . . . . . . . . .> from json a . . . . . . . . . . . . . . .> lateral view json_tuple(a.data,'movie','rate','timeStamp','uid') b as b_movie,b_rate,b_timeStamp,b_uid;
Hive 的 TRANSFORM 关键字提供了在 SQL 中调用自写脚本的功能。适合实现 Hive 中没有的 功能又不想写 UDF 的状况
具体以一个实例讲解。
Json 数据: {"movie":"1193","rate":"5","timeStamp":"978300760","uid":"1"}
需求:把 timestamp 的值转换成日期编号
一、先加载 rating.json 文件到 hive 的一个原始表 rate_json
create table rate_json(line string) row format delimited; load data local inpath '/home/hadoop/rating.json' into table rate_json;
二、建立 rate 这张表用来存储解析 json 出来的字段:
create table rate(movie int, rate int, unixtime int, userid int) row format delimited fields terminated by '\t';
解析 json,获得结果以后存入 rate 表:
insert into table rate select get_json_object(line,'$.movie') as moive, get_json_object(line,'$.rate') as rate, get_json_object(line,'$.timeStamp') as unixtime, get_json_object(line,'$.uid') as userid from rate_json;
三、使用 transform+python 的方式去转换 unixtime 为 weekday
先编辑一个 python 脚本文件
########python######代码 ## vi weekday_mapper.py #!/bin/python import sys import datetime for line in sys.stdin: line = line.strip() movie,rate,unixtime,userid = line.split('\t') weekday = datetime.datetime.fromtimestamp(float(unixtime)).isoweekday() print '\t'.join([movie, rate, str(weekday),userid])
保存文件 而后,将文件加入 hive 的 classpath:
hive>add file /home/hadoop/weekday_mapper.py; hive> insert into table lastjsontable select transform(movie,rate,unixtime,userid) using 'python weekday_mapper.py' as(movie,rate,weekday,userid) from rate;
建立最后的用来存储调用 python 脚本解析出来的数据的表:lastjsontable
create table lastjsontable(movie int, rate int, weekday int, userid int) row format delimited fields terminated by '\t';
最后查询看数据是否正确
select distinct(weekday) from lastjsontable;
补充:hive 读取数据的机制:
一、 首先用 InputFormat<默认是:org.apache.hadoop.mapred.TextInputFormat >的一个具体实 现类读入文件数据,返回一条一条的记录(能够是行,或者是你逻辑中的“行”)
二、 而后利用 SerDe<默认:org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe>的一个具体 实现类,对上面返回的一条一条的记录进行字段切割
Hive 对文件中字段的分隔符默认状况下只支持单字节分隔符,若是数据文件中的分隔符是多 字符的,以下所示:
01||huangbo
02||xuzheng
03||wangbaoqiang
建立表
create table t_bi_reg(id string,name string) row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe' with serdeproperties('input.regex'='(.*)\\|\\|(.*)','output.format.string'='%1$s %2$s') stored as textfile;
导入数据并查询
0: jdbc:hive2://hadoop3:10000> load data local inpath '/home/hadoop/data.txt' into table t_bi_reg; No rows affected (0.747 seconds) 0: jdbc:hive2://hadoop3:10000> select a.* from t_bi_reg a;