Skip to content

Instantly share code, notes, and snippets.

@sdpatil
Created April 7, 2014 00:55
WordCountProcessor.java
package com.spnotes.hadoop;
import org.apache.hadoop.conf.Configured;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WordCountProcessor extends Configured implements Tool {
Logger logger = LoggerFactory.getLogger(WordCountProcessor.class);
public static void main(String[] args)throws Exception {
int exitCode = ToolRunner.run(new WordCountProcessor(), args);
System.exit(exitCode);
}
@Override
public int run(String[] args) throws Exception {
if (args.length != 2) {
System.err.printf("Usage: %s [generic options] <input> <output>\n",
getClass().getSimpleName());
ToolRunner.printGenericCommandUsage(System.err);
return -1;
}
Job job = new org.apache.hadoop.mapreduce.Job();
job.setJarByClass(WordCountProcessor.class);
job.setJobName("WordCounter2");
logger.info("Input path " + args[0]);
logger.info("Oupput path " + args[1]);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setInputFormatClass(KeyValueTextInputFormat.class);
job.setMapperClass(WordCountProcessorMapper.class);
job.setMapOutputValueClass(IntWritable.class);
job.setReducerClass(WordCountProcessorReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
int returnValue = job.waitForCompletion(true) ? 0:1;
System.out.println("job.isSuccessful " + job.isSuccessful());
return returnValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment