|
package com.invic.mapreduce.wordcount;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.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.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
/**
*
* @author lucl
*
*/
public class WordCounterTool extends Configured implements Tool {
private static final Log LOG = LogFactory.getLog(WordCounterTool.class);
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
// 这里需要设置系统参数,否则会包winutils.exe的错误
System.setProperty("hadoop.home.dir", "E:\\hadoop-2.6.0\\hadoop-2.6.0");
try {
int exit = ToolRunner.run(new WordCounterTool(), args);
LOG.info("result : " + exit);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int run(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length < 2) {
LOG.info("Usage: wordcount [...] ");
System.exit(2);
}
Job job = Job.getInstance();
job.setJarByClass(WordCounterTool.class);
job.setMapperClass(MyMapper.class);
job.setCombinerClass(MyReducer.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
for (int i = 0; i < otherArgs.length - 1; ++i) {
FileInputFormat.addInputPath(job, new Path(otherArgs));
}
FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
return job.waitForCompletion(true) ? 0 : 1;
}
}
|
|
|