新版MapReduce的API编程简单模板

新版MapReduce的API编程简单模板

import java.io.IOException;
import java.util.*;

import org.apache.Hadoop.conf.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.util.*;

public class MyJob extends Configured implements Tool{
    public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            StringTokenizer tokenizer = new StringTokenizer(line," \t\n\r\f,.:?![]'`;");
            while (tokenizer.hasMoreTokens()) {
                word.set(tokenizer.nextToken());
              context.write(word, one);
            }
        }
    }

public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            while (values.iterator().hasNext()) {
                sum += values.iterator().next().get();
            }
            context.write(key, new IntWritable(sum));
        }
    }
   
    public int run(String[] args) throws Exception {
     Configuration conf=getConf();
     
        Job  job= new Job(conf,"WordCount");
        job.setJarByClass(MyJob.class);
       
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
       
        job.setJobName("WordCount");
        job.setMapperClass(Map.class);
        job.setCombinerClass(Reduce.class);
        job.setReducerClass(Reduce.class);
       
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        System.exit(job.waitForCompletion(true)?0:1) ;
        return 1;
    }
   
    public static void main(String[] args) throws Exception {
        int res =ToolRunner.run(new Configuration(), new MyJob(), args);
        System.exit(res);
    }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/a667690babdba42e65443fb20af483bc.html