首页 > 其他 > 详细

mapreduce_template

时间:2014-03-11 07:59:16      阅读:446      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
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;
import org.apache.hadoop.util.GenericOptionsParser;

public class test {

  public static class Map extends Mapper<Object, Text, Text, IntWritable>{
    
    private IntWritable one = new IntWritable(1);
    private Text word = new Text();
      
    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
        String inputValue=value.toString();//input
        context.write(word, one);//output
    }
  }
  
  public static class Reduce extends Reducer<Text,IntWritable,Text,Text> {

    private Text result = new Text("reduce");
    public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
       for (IntWritable val : values) {
            val.get();//get number
            val.toString();//get string
            val.toString().getBytes();//get byte[]
        }
      context.write(key, result);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length != 2) {
      System.err.println("Usage: <in> <out>");
      System.exit(2);
    }
    Job job = new Job(conf, "job name");
    job.setJarByClass(test.class);
    job.setMapperClass(Map.class);
    job.setCombinerClass(Reduce.class);
    job.setReducerClass(Reduce.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}
bubuko.com,布布扣

mapreduce_template,布布扣,bubuko.com

mapreduce_template

原文:http://www.cnblogs.com/manhua/p/3591106.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!