一。HIVE streaming
在Hive中,需要实现Hive中的函数无法实现的功能时,就可以用Streaming来实现。其原理可以理解成:用HQL语句之外的语言,如Python、Shell来实现这些功能,同时配合HQL语句,以实现特殊的功能。
二。 实例
1. 日志文件的格式
2014-02-02 01:59:02 W3SVC1 2001:da8:7007:102::244 GET /favicon.ico - 80 - 2001:da8:7007:336:ca:f74b:eede:a024 Mozilla/5.0+(Windows+NT+6.1;+WOW64)+AppleWebKit/537.1+(KHTML,+like+Gecko)+Maxthon/4.1.2.4000+Chrome/26.0.1410.43+Safari/537.1 404 0 2 2014-02-02 01:59:02 W3SVC1 2001:da8:7007:102::244 GET /index.asp - 80 - 2001:da8:7007:336:ca:f74b:eede:a024 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+Trident/7.0;+rv:11.0;+Maxthon/4.1.2.4000) 302 0 0 2014-02-02 01:59:02 W3SVC1 2001:da8:7007:102::244 GET /skin6/index.asp - 80 - 2001:da8:7007:336:ca:f74b:eede:a024 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+Trident/7.0;+rv:11.0;+Maxthon/4.1.2.4000) 200 0 0 2014-02-02 01:59:02 W3SVC1 2001:da8:7007:102::244 GET /skin6/images/head_menu_jt2.gif - 80 - 2001:da8:7007:336:ca:f74b:eede:a024 Mozilla/5.0+(Windows+NT+6.1;+WOW64;+Trident/7.0;+rv:11.0;+Maxthon/4.1.2.4000) 200 0 0
2. 处理的目的
以空格将日志文件分割后,将第10个字段的IP中的’%‘及其后边的数字删除
3.hive脚本和shell内容
hive脚本文件
ADD FILE /home/hadoop_admin/program/bash/process_exmovielog_ipv6.sh; FROM ( FROM exmovielog SELECT TRANSFORM(*) USING 'sh process_exmovielog_ipv6.sh' AS log_date,s_sitename,s_ip,cs_method,cs_uri_stem,cs_uri_query,c_ip,user_agen,sc_status,sc_substatus,sc_win32_status )temp INSERT OVERWRITE TABLE movielog PARTITION (year,month) SELECT *,YEAR(temp.log_date),MONTH(temp.log_date);
process_exmovielog_ipv6.sh脚本内容:
#!/bin/bash #Time : 2016-4-25 #Desc: when do hive SQL , process the ipv6 cat $1 | awk -F " " ' {
#获取%好的位置 pos=index($10,"%"); if(pos == 0) print $1" "$2" "$3" "$4" "$5" "$6" "$7" "$10" "$11" "$12" "$13" "$14; else { ip=substr($10,1,pos-1); print $1" "$2" "$3" "$4" "$5" "$6" "$7" "ip" "$11" "$12" "$13" "$14; } } '