Created
May 4, 2011 11:07
-
-
Save fukata/955075 to your computer and use it in GitHub Desktop.
ApacheLogHadoopExample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.fukata.hadoop; | |
import java.io.IOException; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import org.apache.hadoop.conf.Configuration; | |
import org.apache.hadoop.fs.Path; | |
import org.apache.hadoop.io.IntWritable; | |
import org.apache.hadoop.io.Text; | |
import org.apache.hadoop.mapreduce.Job; | |
import org.apache.hadoop.mapreduce.Mapper; | |
import org.apache.hadoop.mapreduce.Reducer; | |
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; | |
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; | |
import org.apache.hadoop.util.GenericOptionsParser; | |
public class RefererCount { | |
/** | |
* @see http://www.java2s.com/Code/Java/Development-Class/ParseanApachelogfilewithRegularExpressions.htm | |
*/ | |
public static class RefererMapper extends | |
Mapper<Object, Text, Text, IntWritable> { | |
private static final IntWritable ONE = new IntWritable(1); | |
private Text keyReferer = new Text(); | |
private static final int LOG_GROUPS = 9; | |
private static String LOG_REGEX = "^([\\d.]+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (\\d+) \"([^\"]+)\" \"([^\"]+)\""; | |
private static Pattern LOG_PATTERN = Pattern.compile(LOG_REGEX); | |
protected void map( | |
Object key, | |
Text value, | |
org.apache.hadoop.mapreduce.Mapper<Object, Text, Text, IntWritable>.Context context) | |
throws java.io.IOException, InterruptedException { | |
Matcher matcher = LOG_PATTERN.matcher(value.toString()); | |
if (!matcher.matches() || LOG_GROUPS != matcher.groupCount()) { | |
return; | |
} | |
String referer = matcher.group(8); | |
keyReferer.set(referer); | |
context.write(keyReferer, ONE); | |
}; | |
} | |
public static class IntSumReducer extends | |
Reducer<Text, IntWritable, Text, IntWritable> { | |
private IntWritable result = new IntWritable(); | |
public void reduce(Text key, Iterable<IntWritable> values, | |
Context context) throws IOException, InterruptedException { | |
int sum = 0; | |
for (IntWritable val : values) { | |
sum += val.get(); | |
} | |
result.set(sum); | |
context.write(key, result); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
Configuration conf = new Configuration(); | |
String[] otherArgs = new GenericOptionsParser(conf, args) | |
.getRemainingArgs(); | |
if (otherArgs.length != 2) { | |
System.err.println("Usage: org.fukata.hadoop.RefererCount <in> <out>"); | |
System.exit(2); | |
} | |
Job job = new Job(conf, "RefererCount"); | |
job.setJarByClass(RefererCount.class); | |
job.setMapperClass(RefererMapper.class); | |
job.setCombinerClass(IntSumReducer.class); | |
job.setReducerClass(IntSumReducer.class); | |
job.setOutputKeyClass(Text.class); | |
job.setOutputValueClass(IntWritable.class); | |
FileInputFormat.addInputPath(job, new Path(otherArgs[0])); | |
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); | |
System.exit(job.waitForCompletion(true) ? 0 : 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment