-
Notifications
You must be signed in to change notification settings - Fork 262
WordCount Version Three
package org.myorg;
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.net.URI; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; import java.util.regex.Pattern; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; 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.input.FileSplit; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; import org.apache.log4j.Logger;
public class WordCount extends Configured implements Tool {
private static final Logger LOG = Logger.getLogger(WordCount.class);
public static void main(String[] args) throws Exception { int res = ToolRunner.run(new WordCount(), args); System.exit(res); }
public int run(String[] args) throws Exception { Job job = Job.getInstance(getConf(), "wordcount");
for (int i = 0; i < args.length; i += 1) {
if ("-skip".equals(args[i])) {
job.getConfiguration().setBoolean("wordcount.skip.patterns", true);
i += 1;
job.addCacheFile(new Path(args[i]).toUri());
// this demonstrates logging
LOG.info("Added file to the distributed cache: " + args[i]);
}
}
job.setJarByClass(this.getClass());
// Use TextInputFormat, the default unless job.setInputFormatClass is used
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
return job.waitForCompletion(true) ? 0 : 1;
}
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); private boolean caseSensitive = false; private long numRecords = 0; private String input; private Set patternsToSkip = new HashSet();
private static final Pattern WORD_BOUNDARY = Pattern.compile("\\s*\\b\\s*");
protected void setup(Mapper.Context context)
throws IOException,
InterruptedException {
if (context.getInputSplit() instanceof FileSplit) {
this.input = ((FileSplit) context.getInputSplit()).getPath().toString();
} else {
this.input = context.getInputSplit().toString();
}
Configuration config = context.getConfiguration();
this.caseSensitive = config.getBoolean("wordcount.case.sensitive", false);
if (config.getBoolean("wordcount.skip.patterns", false)) {
URI[] localPaths = context.getCacheFiles();
parseSkipFile(localPaths[0]);
}
}
private void parseSkipFile(URI patternsURI) {
LOG.info("Added file to the distributed cache: " + patternsURI);
try {
BufferedReader fis = new BufferedReader(new FileReader(new File(patternsURI.getPath()).getName()));
String pattern;
while ((pattern = fis.readLine()) != null) {
patternsToSkip.add(pattern);
}
} catch (IOException ioe) {
System.err.println("Caught exception while parsing the cached file '"
+ patternsURI + "' : " + StringUtils.stringifyException(ioe));
}
}
public void map(LongWritable offset, Text lineText, Context context)
throws IOException, InterruptedException {
String line = lineText.toString();
if (!caseSensitive) {
line = line.toLowerCase();
}
Text currentWord = new Text();
for (String word : WORD_BOUNDARY.split(line)) {
if (word.isEmpty() || patternsToSkip.contains(word)) {
continue;
}
currentWord = new Text(word);
context.write(currentWord,one);
}
if ((++numRecords % 100) == 0) {
context.setStatus("Finished processing " + numRecords + " records "
+ "from the input: " + input);
}
}
}
public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> { @Override public void reduce(Text word, Iterable counts, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable count : counts) { sum += count.get(); } context.write(word, new IntWritable(sum)); } } }