Welcome 微信登录

首页 / 数据库 / MySQL / 使用Hive的正则解析器RegexSerDe分析Nginx日志

1、环境:Hadoop-2.6.0 + apache-hive-1.2.0-bin2、使用Hive分析nginx日志,网站的访问日志部分内容为:cat /home/hadoop/hivetestdata/nginx.txt
192.168.1.128 - - [09/Jan/2015:12:38:08 +0800] "GET /avatar/helloworld.png HTTP/1.1" 200 1521 "http://write.blog.linuxidc.net/postlist" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36"
183.60.212.153 - - [19/Feb/2015:10:23:29 +0800] "GET /o2o/media.html?menu=3 HTTP/1.1" 200 16691 "-" "Mozilla/5.0 (compatible; baiduuSpider; +http://www.baiduu.com/search/spider.html)"这条日志里面有九列,每列之间是用空格分割的,
每列的含义分别是客户端访问IP、用户标识、用户、访问时间、请求页面、请求状态、返回文件的大小、跳转来源、浏览器UA。
我们使用Hive中的正则表达式匹配这九列:
([^ ]*) ([^ ]*) ([^ ]*) (.∗) (".*?") (-|[0-9]*) (-|[0-9]*) (".*?") (".*?")
于此同时我们可以在Hive中指定解析文件的序列化和反序列化解析器(SerDe),并且在Hive中内置了一个org.apache.hadoop.hive.serde2.RegexSerDe正则解析器,我们可以直接使用它。3、建表语句 
CREATE TABLE logs

host STRING,
identity STRING,
username STRING,
time STRING,
request STRING,
status STRING,
size STRING,
referer STRING,
agent STRING

ROW FORMAT SERDE "org.apache.hadoop.hive.serde2.RegexSerDe"
WITH SERDEPROPERTIES (
"input.regex" = "([^ ]*) ([^ ]*) ([^ ]*) (\[.*\]) (".*?") (-|[0-9]*) (-|[0-9]*) (".*?") (".*?")",
"output.format.string" = "%1$s %2$s %3$s %4$s %5$s %6$s %7$s %8$s %9$s"

STORED AS TEXTFILE;4、加载数据: 
load data local inpath "/home/hadoop/hivetestdata/nginx.txt" into table logs;
 
查询每小时的访问量超过100的IP地址: 
select substring(time, 2, 14) datetime ,host, count(*) as count
from logs
group by substring(time, 2, 14), host
having count > 100sort by datetime, count;Hive编程指南 PDF 中文高清版  http://www.linuxidc.com/Linux/2015-01/111837.htm基于Hadoop集群的Hive安装 http://www.linuxidc.com/Linux/2013-07/87952.htmHive内表和外表的区别 http://www.linuxidc.com/Linux/2013-07/87313.htmHadoop + Hive + Map +reduce 集群安装部署 http://www.linuxidc.com/Linux/2013-07/86959.htmHive本地独立模式安装 http://www.linuxidc.com/Linux/2013-06/86104.htmHive学习之WordCount单词统计 http://www.linuxidc.com/Linux/2013-04/82874.htmHive运行架构及配置部署 http://www.linuxidc.com/Linux/2014-08/105508.htmHive 的详细介绍:请点这里
Hive 的下载地址:请点这里本文永久更新链接地址