MapReduce 실례 워드 카운트

package xlzx.mapreduce;

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.output.FileOutputFormat;

import java.io.IOException;

public class WordCount {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException{
        if(args.length < 2){
            System.out.println("There can be only two input and output parameters");
        }
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "word count");
        job.setJarByClass(WordCount.class);

        job.setMapperClass(WordMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setReducerClass(WordReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }


    public static class WordMapper extends Mapper{
        protected void map(LongWritable key, Text value1, Context context) throws
                IOException, InterruptedException {
            String line = value1.toString();
            String[] words = line.split(" ");
            for(String word :words){
                context.write(new Text(word), new IntWritable(1));
            }
        }
    }


    public static class WordReducer extends Reducer{
        protected void reduce(Text key, Iterable value2, Context context) throws
        IOException, InterruptedException{
            int sum = 0;

            for(IntWritable v3 :value2){
                sum += v3.get();
            }
            context.write(key, new IntWritable(sum));
        }
    }
}

좋은 웹페이지 즐겨찾기