WordCount

package org.apache.hadoop.examples;

import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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 MyWordCount
{
     //main方法
     public static void main(String[] args) throws Exception
     {
        Configuration conf = new Configuration();
        final String OUTPUT_PATH = "output";   

        String[] otherArgs = new String[]{"input","output"};

        Path path = new Path(OUTPUT_PATH);  
        FileSystem fileSystem = path.getFileSystem(conf);       
        if (fileSystem.exists(new Path(OUTPUT_PATH)))
        {  
           fileSystem.delete(new Path(OUTPUT_PATH),true);  
        }  

        if(otherArgs.length < 2)
        {
            System.err.println("Usage: wordcount <in> [<in>...] <out>");
            System.exit(2);
        }
        Job job = Job.getInstance(conf, "word count");  //Job(Configuration conf, String jobName) 设置job名称
        job.setJarByClass(WordCount.class);
        job.setMapperClass(WordCount.TokenizerMapper.class);  //为job设置Mapper类
        job.setCombinerClass(WordCount.IntSumReducer.class);  //为job设置Combiner类
        job.setReducerClass(WordCount.IntSumReducer.class);  //为job设置Reduce类

        job.setOutputKeyClass(Text.class);  //设置输出key的类型
        job.setOutputValueClass(IntWritable.class); //设置输出value的类型

        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  //为map-reduce任务设置InputFormat实现类   设置输入路径
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  //为map-reduce任务设置OutputFormat实现类  设置输出路径
        System.exit(job.waitForCompletion(true)?0:1);
    }

    //Map类,继承自Mapper类--一个抽象类
    public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>
    {
        private static final IntWritable one = new IntWritable(1);
        private Text word = new Text();   //Text 实现了BinaryComparable类可以作为key值
        public TokenizerMapper()
        {
        }

        public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException
        {
            StringTokenizer itr = new StringTokenizer(value.toString()); //得到什么值   StringTokenizer是分割String串的方法
            while(itr.hasMoreTokens())
            {
                this.word.set(itr.nextToken());
                context.write(this.word, one);
            }
        }
    }

  //Reduce类,继承自Reducer类--一个抽象类
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable>
{
        private IntWritable result = new IntWritable();
        public IntSumReducer() {}
        public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException
        {
            int sum = 0;

            IntWritable val;
            for(Iterator i = values.iterator(); i.hasNext(); sum += val.get())
            {
                val = (IntWritable)i.next();
            }
            this.result.set(sum);
            context.write(key, this.result);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/xiedelong/article/details/80171206