使用Hive的正則解析器RegexSerDe分析Nginx日誌

來源:互聯網
上載者:User

使用Hive的正則解析器RegexSerDe分析Nginx日誌

1、環境:

Hadoop-2.6.0 + apache-hive-1.2.0-bin

2、使用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.bkjia.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中的Regex匹配這九列:
([^ ]*) ([^ ]*) ([^ ]*) (.∗) (\".*?\") (-|[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 > 100

sort by datetime, count;

Hive編程指南 PDF 中文高清版 

基於Hadoop叢集的Hive安裝

Hive內表和外表的區別

Hadoop + Hive + Map +reduce 叢集安裝部署

Hive本地獨立模式安裝

Hive學習之WordCount單詞統計

Hive運行架構及配置部署

Hive 的詳細介紹:請點這裡
Hive 的:請點這裡

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.