2017-06-11 54 views
0

我有一個具有以下格式的日誌文件,我想從中提取ip, datetime and uri並加載到表中。如何使用RegexSerDe將日誌文件加載到Hive表中?

64.242.88.10 - - [07/Mar/2004:17:09:01 -0800] "GET /twiki/bin/search/Main/SearchResult?scope=text&search=Joris%20*Benschop[^A-Za-z] HTTP/1.1" 200 4284 

我能夠做到這一點的裝載日誌文件行作爲一個字符串到表如下,並通過使用regexp_extract

create table logs(line string);

load data local inpath '.../mylog.log' into table logs;

select regexp_extract(line, '(.*) (- -) \\[(.*) -.*\\] \\"GET (.*)\\?',1),--ip regexp_extract(line, '(.*) (- -) \\[(.*) -.*\\] \\"GET (.*)\\?',3),--datetime regexp_extract(line, '(.*) (- -) \\[(.*) -.*\\] \\"GET (.*)\\?',4) --uri from logs limit 10;

+---------------+-----------------------+--------------------------------------------+--+ 
|  _c0  |   _c1   |     _c2      | 
+---------------+-----------------------+--------------------------------------------+--+ 
| 64.242.88.10 | 07/Mar/2004:17:09:01 | /twiki/bin/search/Main/SearchResult  | 
| 64.242.88.10 | 07/Mar/2004:17:10:20 | /twiki/bin/oops/TWiki/TextFormattingRules | 
+---------------+-----------------------+--------------------------------------------+--+ 

我想要做的是創建一個表,指定SERDE性能並加載它,而無需使用regexp_extract功能。我嘗試了下面,但沒有工作。

create table logs (
    ip string, 
    day timestamp, 
    url string) 
    row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe' 
    with serdeproperties ("input.regex" = 
    "(.*) [^- - \[](.*) [^-.*\]] \"([^GET].*\?)"); 

load data local inpath ".../mylog.log" into table logs; 

我欣賞一些幫助和指導。

+0

您SERDE不匹配先前使用的正則表達式。你現在試圖捕獲'GET'。另外,您是否只想捕獲GET請求? –

+0

該文檔似乎正是你想要的... https://cwiki.apache.org/confluence/display/Hive/GettingStarted#GettingStarted-ApacheWeblogDataData –

回答

1
  1. day不能時間戳,因爲它不是以ISO格式(yyyy-MM-dd HH:mm:ss
  2. 逃逸應帶有雙反斜線(\\
  3. 做正則表達式應該覆蓋整個記錄(在這種情況下,終止與.*

create external table logs 
(
    ip string 
    ,day string 
    ,url string 
) 
    row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe' 
    with serdeproperties ("input.regex" = "(\\S+).*?\\[(.*?)\\s.*?(/.*?)\\?.*") 
; 

select * from logs 
; 

+--------------+----------------------+-------------------------------------+ 
|  ip  |   day   |     url     | 
+--------------+----------------------+-------------------------------------+ 
| 64.242.88.10 | 07/Mar/2004:17:09:01 | /twiki/bin/search/Main/SearchResult | 
+--------------+----------------------+-------------------------------------+ 
+0

檢查更新的答案(更多來...) –

+0

啊哈,錯過了那一點關於'一天'。那就是爲什麼我看到NULL。 – Bala

相關問題