日志格式
61.160.241.107 - - [23/Aug/2011:22:00:00 +0800] "GET /?gid=38&sid=75&user=14717213&roleid=490711&time=1314108000&user_yx=736959&levafee11f0d1bacbfecbb631192 HTTP/1.1" 200 5 "-" "Java/1.6.0_23"
以对IP 的访问量进行统计为例
map脚本 清洗日志数据
#!/usr/bin/python
import sys
import re
debug = False#设置lzo文件偏移位
if debug:
lzo = 0
else:
lzo = 1
for line in :
ipaddress=(r'([\d.]*) (- - \[[^[\]]*\] "[^ ]* /)([^ ]*)([^ ]*\.php\?)([^ ]*)')
match=(('\t',1)[lzo])
if match:
ip=(1)
#tb=(2)
#url=(4)
print ip
reduce脚本 对ip数进行统计
#!/usr/bin/python
#-*-coding:UTF-8 -*-
import sys
import os
import string
res = {}
for line in :
skey=line[0:-1]
if(res.has_key(skey)==False):
res[skey]=0
res[skey]=res[skey]+1
for key in ():
print key+"\t"+str(res[key])
将 脚本修改为可执行权限
# chmod +x *.py
在shell中进行调试
# cat |/home/|/home/
在hadoop streaming中执行mapreduce作业
# hadoop jar /opt/mapr/hadoop/hadoop-0.20.2/contrib/streaming/hadoop-0.20. -file /home/ -file /home/ -mapper /home/ -reducer /homej/ -input /test/.-output /test/test2
其中input 与output 路径都为hdfs文件系统路径 如果将输出文件进行压缩的话需要加参数 -jobconf =true -jobconf =
如果输入文件格式为lzo压缩格式的话 需要指定参数-inputformat 注意的是在hadoop中使用lzo的话,偏移位需要加1