我想从访问日志中提取出(ip,requestUrl,timeStamp)以加载到hive数据库.访问日志中的一行如下.
66.249.68.6 - - [14/Jan/2012:06:25:03 -0800] "GET /example.com HTTP/1.1" 200 708 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
我尝试使用以下几种正则表达式而没有任何成功.(加载的表包含所有NULL值,表示正则表达式与输入不匹配).
CREATE TABLE access_log (
remote_ip STRING,
request_date STRING,
method STRING,
request STRING,
protocol STRING
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.contrib.serde2.RegexSerDe'
WITH SERDEPROPERTIES (
"input.regex" = "([^ ]) . . [([^]]+)] \"([^ ]) ([^ ]) ([^ \"])\" *",
"output.format.string" = "%1$s %2$s %3$s %4$s %5$s"
)
STORED AS TEXTFILE;
我对正则表达式不是很有经验.任何人都可以帮我吗?
小智 11
最后使用双'\'和'.*'(这很重要!):
CREATE EXTERNAL TABLE access_log (
`ip` STRING,
`time_local` STRING,
`method` STRING,
`uri` STRING,
`protocol` STRING,
`status` STRING,
`bytes_sent` STRING,
`referer` STRING,
`useragent` STRING
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.contrib.serde2.RegexSerDe'
WITH SERDEPROPERTIES (
'input.regex'='^(\\S+) \\S+ \\S+ \\[([^\\[]+)\\] "(\\w+) (\\S+) (\\S+)" (\\d+) (\\d+) "([^"]+)" "([^"]+)".*'
)
STORED AS TEXTFILE
LOCATION '/tmp/access_logs/';
Run Code Online (Sandbox Code Playgroud)
PS Hive 0.7.1
我用rubular来测试我的正则表达式.您也可以使用此表达式
([^ ]*) ([^ ]*) ([^ ]*) (?:-|\[([^\]]*)\]) ([^ \"]*|\"[^\"]*\") (-|[0-9]*)
Run Code Online (Sandbox Code Playgroud)
您将获得以下输出
1. 66.249.68.6
2. -
3. -
4. 14/Jan/2012:06:25:03 -0800
5. "GET /example.com HTTP/1.1"
6. 200
Run Code Online (Sandbox Code Playgroud)