Skip to content

Instantly share code, notes, and snippets.

@sdpatil
Last active August 29, 2015 13:58
Show Gist options
  • Select an option

  • Save sdpatil/10010382 to your computer and use it in GitHub Desktop.

Select an option

Save sdpatil/10010382 to your computer and use it in GitHub Desktop.
WordCount Mapper class
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.LongWritable;
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.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WordCountDriver extends Configured implements Tool{
Logger logger = LoggerFactory.getLogger(WordCountDriver.class);
public static void main(String[] args) throws Exception{
int exitCode = ToolRunner.run(new WordCountDriver(), args);
System.exit(exitCode);
}
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(WordCountDriver.class);
job.setJobName("WordCounter");
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.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.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