hadoop官网的wordcount案例(Example: WordCount v1.0)

官网的wordcount:链接: link
https://hadoop.apache.org/docs/stable/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html

Input and Output types of a MapReduce job:

(input) -> map -> -> combine -> -> reduce -> (output)

==mapreduce:==以map的形式输入,默认以map
输入到reduce,经过自己的处理,继续以map的形式输入,但每次的map形式都是按照自己的目的走的。下面分析一下这段代码

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;

public class WordCount {

  public static class TokenizerMapper
       extends Mapper<Object, Text, Text, IntWritable>{
       //Object:代表输入的map的key,默认是偏移量
       //Text:代表输入的map的value,默认是读取的一行字符串
       //Text:代表输出的map的key,是已经分割的每个单词
       //IntWritable:代表输出的map的value,计数为1

    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 itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, one);
      }
    }
  }

  public static class IntSumReducer
       extends Reducer<Text,IntWritable,Text,IntWritable> {
        //Text:代表输入的map的key,是每个单词
       //IntWritable:代表输入的map的value,map以后的value
       //Text:代表输出的map的key,展示的结果
       //IntWritable:代表输出的map的value,每个单词出现的总和
    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();
    //构建出一个job对象并且给这个job命名一个名字
    Job job = Job.getInstance(conf, "word count");
    //运行job加载的主类
    job.setJarByClass(WordCount.class);
    //运行job加载的map类
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
     //运行job加载的map类
    job.setReducerClass(IntSumReducer.class);
    //最终输出map键的类型
    job.setOutputKeyClass(Text.class);
    //最终map输出value的类型
    job.setOutputValueClass(IntWritable.class);
    //分布式文件输入的路径
    FileInputFormat.addInputPath(job, new Path(args[0]));
    //分布式文件结果输出的路径
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    //三目运算符表示程序是否执行成功
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}


打包在客户端运行
Compile WordCount.java and create a jar:

$ bin/hadoop com.sun.tools.javac.Main WordCount.java
$ jar cf wc.jar WordCount*.class
Assuming that:
将文件上传到分布式文件系统
/user/joe/wordcount/input - input directory in HDFS
/user/joe/wordcount/output - output directory in HDFS
Sample text-files as input:

查看文件是否上传成功
$ bin/hadoop fs -ls /user/joe/wordcount/input/
/user/joe/wordcount/input/file01
/user/joe/wordcount/input/file02
查看文件类容
$ bin/hadoop fs -cat /user/joe/wordcount/input/file01
Hello World Bye World

$ bin/hadoop fs -cat /user/joe/wordcount/input/file02
Hello Hadoop Goodbye Hadoop
Run the application:

执行命令

$ bin/hadoop jar wc.jar WordCount /user/joe/wordcount/input /user/joe/wordcount/output
Output:

查看执行结果

$ bin/hadoop fs -cat /user/joe/wordcount/output/part-r-00000
Bye 1
Goodbye 1
Hadoop 2
Hello 2
World 2

你可能感兴趣的:(hadoop)