Hadoop2.7.1-WordCount Demo

package mytest.hadoop.mr1;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

  public static class TokenizerMapper 
       extends Mapper<Object, Text, Text, IntWritable>{
    
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();
      
    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
      //StringTokenizer 是出于兼容性的原因而被保留的遗留类
      StringTokenizer itr = new StringTokenizer(value.toString());//被分割对象str,分隔符采取默认分割,java默认的分隔符是“空格”、“制表符(‘\t’)”、“换行符(‘\n’)”、“回车符(‘\r’)”。默认的话,所有的分隔符都会同时起作用。
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, one);
      }
    }
  }
  
  public static class IntSumReducer 
       extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values, 
                       Context context
                       ) throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable val : values) {
        sum += val.get();
      }
      result.set(sum);
      context.write(key, result);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length < 2) {
      System.err.println("Usage: wordcount <in> [<in>...] <out>");
      System.exit(2);
    }
    System.out.println("otherArgs.length="+otherArgs.length);
    for (int i = 0; i < otherArgs.length; ++i) {
        System.out.println(i+"--"+otherArgs[i].toString());
      }
    System.out.println("args over");
    
    //job.jar在job路径下的位置
    conf.set("mapred.jar","E:\\wc.jar");//必需的!!!!
    //跨平台提交作业
    conf.set("mapreduce.app-submission.cross-platform","true");//必需的!!!!$JAVA_HOME VS %JAVA_HOME%
    //分布式文件 URI
    conf.set("fs.defaultFS", "hdfs://master:9000");//必需的!!!!
    //conf.set("mapreduce.jobtracker.address", "master"); 
    conf.set("mapreduce.framework.name", "yarn");  //必需的!!!!
    conf.set("yarn.resourcemanager.address", "master:8032"); //必需的!!!!
    
    Job job = Job.getInstance(conf, "word count");
    job.setJarByClass(WordCount.class);
    //设置map
    job.setMapperClass(TokenizerMapper.class);
    //设置Combine.Combiner使得map task与reduce task之间的数据传输量大大减小,可明显提高性能。大多数情况下,Combiner与Reducer相同
    job.setCombinerClass(IntSumReducer.class);
    //设置reduce
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    //设置输入输出
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
    FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));
    System.out.println("222-------------111");
    //提交作业并等待其执行结束。在这里主要通过submit()方法提交一个作业。
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

 

你可能感兴趣的:(hadoop,wordcount,mr)