首页 技术 正文
技术 2022年11月17日
0 收藏 1000 点赞 4,633 浏览 2631 个字

以下案例是使用hive分析nginx的访问日志案例,其中字段分隔通过正则表达式匹配,具体步骤如下:

日志格式:
192.168.5.139 – – [08/Jun/2017:17:09:12 +0800] “GET //oportal/static/ui/layer/skin/default/icon.png HTTP/1.1” 200 9905 http://192.168.100.126//oportal/static/ui/layer/skin/layer.css “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36” –
192.168.5.139 – – [08/Jun/2017:17:09:25 +0800] “GET //oportal/page/homepage/images/icon-02.png HTTP/1.1” 200 1322 http://192.168.100.126//dsfdsal/page/homepage/css/indet.css “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36” –
192.168.5.139 – – [08/Jun/2017:17:09:25 +0800] “GET /dsfdortal/page/waittodo/waittodo.jsp?registesfsdame=%25E7%25BB%25BC%25E5%sdf2590%2588%25E9%25A2%2584%25E7%25AE%25sdf97&registerAsdfsdppid=bsdfsdas,ssdfsdfpf,bsdfsdgt,insdfsddi,hqrsdfdseport,hqosdfa,hqsfdsbi&resdfgisterId=FD748AA3sd82851A37F1693D3880C844EF&allviewsdfnum=10&appSource=undefined&tokenid=5728A0ED7998CC84B88FE8717A33FAB8aK79UkfS&waittodoNums=0&showway=0 HTTP/1.1” 200 3121 http://192.168.100.126//fposdfsdrtal/page/homdsfdepage/homepage.jsp?tokenid=5728A0ED7998CC84B88FE8717A33FAB8aK79UkfS “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36” –
192.168.5.139 – – [08/Jun/2017:17:09:25 +0800] “GET //fposdfgfrtal/page/waittodo/css/db_index.css HTTP/1.1” 200 6310 http://192.168.100.126/fpdsfdsfortal/page/waittodo/waittodo.jsp?registerName=%25E7%25BB%25BC%25E5%2590%2588%25E9%25A2%2584%25E7%25AE%2597&registerAppid=bas,spf,bgt,indi,hqreport,hqoa,hqbi&registerId=FD748AA382851A37F1693D3880C844EF&allviewnum=10&appSource=undefined&tokenid=5728A0ED7998CC84B88FE8717A33FAB8aK79UkfS&waittodoNums=0&showway=0 “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36” –

正则匹配:
测试网站:http://wpjam.qiniudn.com/tool/regexpal/

([^ |^\n]*) ([^ ]*) ([^ ]*) (\[.*\]) (\”.*?\”) (-|[0-9]*) (-|[0-9]*) (\”.*?\”) (\”.*?\”) (-)

建立原表:

drop table if exists chavin.nginx_access_log;
CREATE TABLE chavin.nginx_access_log(
   host STRING,
   identity STRING,
   user STRING,
   time STRING,
   request STRING,
   status STRING,
   size STRING,
   referer STRING,
   agent STRING,
   other STRING)
ROW FORMAT SERDE ‘org.apache.hadoop.hive.contrib.serde2.RegexSerDe’
WITH SERDEPROPERTIES (
   “input.regex” = “([^ |^\n]*) ([^ ]*) ([^ ]*) (\\[.*\\]) (\”.*?\”) (-|[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 %10$s”
)
STORED AS TEXTFILE;

清除access.log日志中空白行:

sed -i ‘/^$/d’ access.log

加载access.log日志数据到hive中:

load data local inpath ‘/opt/datas/access.log’ overwrite into table chavin.nginx_access_log;

查询数据,进行验证:

select * from chavin.nginx_access_log limit 5;

接下来可以建立业务子表,进行定向分析了。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898