北极星光 发表于 2018-10-30 08:55:09

Hadoop2.6.0学习笔记(二)MapReduce通过Eclipse运行

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));
  
      return job.waitForCompletion(true) ? 0 : 1;
  
    }
  

  
}


页: [1]
查看完整版本: Hadoop2.6.0学习笔记(二)MapReduce通过Eclipse运行