天天看點

Hadoop WordCount示例及源碼解析

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

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

/**
 * FileName: MyFirstMapReduce
 * Author:   hadoop
 * Email:    [email protected]
 * Date:     18-10-5 下午1:21
 * Description:
 */
public class MyFirstMapReduce {
    public static class WordCountMap extends
            Mapper<LongWritable, Text, Text, IntWritable> {
        private final IntWritable one = new IntWritable(1);
        private Text word = new Text();

        //架構在遇到資料分片split的每一條記錄的時候都會回調該方法來進行具體的業務處理
        public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            String line = value.toString();
            StringTokenizer token = new StringTokenizer(line);
            while (token.hasMoreTokens()) {
                word.set(token.nextToken());
                context.write(word, one);
            }
        }
    }

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

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = new Job(conf);
        job.setJarByClass(WordCount.class);
        job.setJobName("wordcount");
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setMapperClass(WordCountMap.class);
        job.setReducerClass(WordCountReduce.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        job.waitForCompletion(true);
    }
}
           

    1.每個hadoop程式通常情況下都由Mapper和Reducer構成;

    2.每次在mapper中覆寫的map方法都會被Mapper類的run方法通過while循環基于目前Mapper所處理的資料分片split反複調用map(調用的時候會把每一行的Key和value的内容傳給map),而子類的map中就實作了具體業務邏輯處理;

     public void run(Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>.Context context) throws IOException, InterruptedException {

            this.setup(context);

            try {

                while(context.nextKeyValue()) {

                    this.map(context.getCurrentKey(), context.getCurrentValue(), context);

                }

            } finally {

                this.cleanup(context);

            }

        }

    3.Mapper子類中map的輸出的key和value的類型及時Reducer子類中reduce的輸入key和value(此處的value一定是Mapper中輸出value類型的集合)的類型;

    4.源碼是最大的捷徑,是一切問題的答案;