一步一步学习hadoop(十)

来源:互联网 发布:淘宝网店怎么开步骤 编辑:程序博客网 时间:2024/04/26 06:18

MapRedcue作业的设置与运行

       新版本的设置和旧版本的有较大区别,新版本使用job对象统一管理作业的配置和运行,删除了JobClient对象,实现了配置和运行的统一。

       编写好了map函数和reduce函数,再对MapReduce作业进行适当的设置,MapReduce作业就可以在Hadoop框架上运行了。以一个简单的例子来讲解MapReduce作业的设置。该作业是Hadoop框架的一个例子,读取文本文件,统计每个单词出现的次数。

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 WordCount {  //定义map函数  public static class TokenizerMapper       extends Mapper<Object, Text, Text, IntWritable>{        private final static IntWritable one = new IntWritable(1);    private Text word = new Text();          public void map(Object key, Text value, Context context                    ) throws IOException, InterruptedException {      StringTokenizer itr = new StringTokenizer(value.toString());      while (itr.hasMoreTokens()) {        word.set(itr.nextToken());        context.write(word, one);      }    }  }  //定义reduce函数  public static class IntSumReducer       extends Reducer<Text,IntWritable,Text,IntWritable> {    private IntWritable result = new IntWritable();    public void reduce(Text key, Iterable<IntWritable> values,                       Context context                       ) throws IOException, InterruptedException {      int sum = 0;      for (IntWritable val : values) {        sum += val.get();      }      result.set(sum);      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: wordcount <in> <out>");      System.exit(2);    }    //创建作业    Job job = new Job(conf, "word count");    job.setJarByClass(WordCount.class);    //设置mapper类    job.setMapperClass(TokenizerMapper.class);    //设置合并类,是一个优化措施,下一节讲述    job.setCombinerClass(IntSumReducer.class);    //设置reducer类    job.setReducerClass(IntSumReducer.class);    //设置作业输出的键的类型。    job.setOutputKeyClass(Text.class);    //设置作业输出的值的类型    job.setOutputValueClass(IntWritable.class);    //设置作业处理的文件的路径,如果是目录,则对下面所有的文件进行处理    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));    //设置作业结果保存的目录,该目录必须在HDFS中不存在否则,作业会报错    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));    //运行作业    System.exit(job.waitForCompletion(true) ? 0 : 1);  }}
几点说明:
1.如果map输出的key/value的类型和作业输出的key/value类型不一样,则要通过setMapOutputKeyClass和setMapOutputValueClass进行设置
2.如果要运行多个Reduce任务必须通过setNumReduceTasks显式设置,否则默认为一个reduce,当然也可以设置为0个Reduce任务,这时候作业完全并行。
3.setInputFormatClass设置对输入数据的解析方式,以前已经讲述过了
4.setOutputFormatClass设置作业输出数据怎样保存的,以后再讲。
5.setPartitionerClass,设置map的输出到reduce任务的映射,下一节讲述。


原创粉丝点击