本人刚学习,这是开发的第一个复杂一点的程序。既当作自己的个人笔记,也算是分享一下。
数据类型
199.72.81.55 - - [01/Jul/1995:00:00:01 -0400] "GET /history/apollo/ HTTP/1.0" 200 6245
ip 时间 访问 状态码 大小
首先我自定义一个数据类型LogWritable,里面包含userIP, timestamp, request,status,responseSize。
定义一个LogFileInputFormat,作为数据进入map函数的处理,重写createRecordReader()和getSplits()方法,实现一个LogRecordReader类,用正则表达式分析数据"^(\\S+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (\\d+)"取出ip和大小,
String userIP = matcher.group(1);
String timestamp = matcher.group(4);
String request = matcher.group(5);
int status = Integer.parseInt(matcher.group(6));
int bytes = Integer.parseInt(matcher.group(7));
map类将ip和大小输出context.write(value.getUserIP(),value.getResponseSize());
IPBasedPartitioner通过模运算类把同一个ip的负责发送到一个reduce类里面token.hashCode() & Integer.MAX_VALUE) % numPartitions
reduce类负责把大小加起来就可以了
for (IntWritable val : values) {
sum += val.get();
}
输出就可以了
新人学习 求大神带我飞
数据类型
199.72.81.55 - - [01/Jul/1995:00:00:01 -0400] "GET /history/apollo/ HTTP/1.0" 200 6245
ip 时间 访问 状态码 大小
首先我自定义一个数据类型LogWritable,里面包含userIP, timestamp, request,status,responseSize。
定义一个LogFileInputFormat,作为数据进入map函数的处理,重写createRecordReader()和getSplits()方法,实现一个LogRecordReader类,用正则表达式分析数据"^(\\S+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (\\d+)"取出ip和大小,
String userIP = matcher.group(1);
String timestamp = matcher.group(4);
String request = matcher.group(5);
int status = Integer.parseInt(matcher.group(6));
int bytes = Integer.parseInt(matcher.group(7));
map类将ip和大小输出context.write(value.getUserIP(),value.getResponseSize());
IPBasedPartitioner通过模运算类把同一个ip的负责发送到一个reduce类里面token.hashCode() & Integer.MAX_VALUE) % numPartitions
reduce类负责把大小加起来就可以了
for (IntWritable val : values) {
sum += val.get();
}
输出就可以了
新人学习 求大神带我飞
