• 新增的Java MapReduce API


    http://book.51cto.com/art/201106/269647.htm

    Hadoop的版本0.20.0包含有一个新的 Java MapReduce API,有时也称为"上下文对象"(context object),旨在使API在今后更容易扩展。新的API 在类型上不兼容先前的API,所以,需要重写以前的应用程序才能使新的API发挥作用。

    新增的API 和旧的API 之间,有下面几个明显的区别。

    新的API 倾向于使用虚类,而不是接口,因为这更容易扩展。例如,可以无需修改类的实现而在虚类中添加一个方法(即用默认的实现)。在新的API 中, mapper 和reducer现在都是虚类。

    新的API 放在org.apache.hadoop.mapreduce 包(和子包)中。之前版本的API 依旧放在org.apache.hadoop.mapred中。

    新的API充分使用上下文对象,使用户代码能与MapReduce系统通信。例如,MapContext 基本具备了JobConf、OutputCollector和Reporter的功能。

    新的API 同时支持"推"(push)和"拉"(pull)式的迭代。这两类API,均可以将键/值对记录推给mapper,但除此之外,新的API 也允许把记录从map()方法中拉出。对reducer来说是一样的。"拉"式处理数据的好处是可以实现数据的批量处理,而非逐条记录地处理。

    新增的API实现了配置的统一。旧API 通过一个特殊的JobConf 对象配置作业,该对象是Hadoop配置对象的一个扩展 (用于配置守护进程,详情请参见第130页的"API配置"小节)。在新的API 中,我们丢弃这种区分,所有作业的配置均通过Configuration 来完成。

    新API中作业控制由Job类实现,而非JobClient类,新API中删除了JobClient类。

    输出文件的命名方式稍有不同。map的输出文件名为part-m-nnnnn,而reduce的输出为part-r-nnnnn(其中nnnnn表示分块序号,为整数,且从0开始算)。

    例2-6 显示了使用新API 重写的MaxTemperature应用。不同之处已加粗显示。

     将旧API写的Mapper和Reducer类转换为新API时,记住将map()和reduce()的签名转换为新形式。如果只是将类的继承修改为对新的Mapper和Reducer类的继承,编译的时候也不会报错或显示警告信息,因为新的Mapper和Reducer类同样也提供了等价的map()和reduce()函数。但是,自己写的mapper或reducer代码是不会被调用的,这会导致难以诊断的错误。

    例2-6. 用新上下文对象MapReduce API重写的MaxTemperature应用

    1. public class NewMaxTemperature {  
    2.  
    3.   static class NewMaxTemperatureMapper  
    4.     extends Mapper<LongWritable, Text, Text, IntWritable> {  
    5.  
    6.     private static final int MISSING = 9999;  
    7.     public void map(LongWritable key, Text value,
      Context context  
    8.         throws IOException, InterruptedException {               
    9.  
    10.     String line = value.toString();         
    11.     String year = line.substring(15, 19);         
    12.     int airTemperature;         
    13.     if (line.charAt(87) == '+') { // parseInt
      doesn't like leading plus signs          
    14.       airTemperature = Integer.parseInt(line.substring(88, 92));         
    15.     } else {           
    16.       airTemperature = Integer.parseInt(line.substring(87, 92));         
    17.     }         
    18.     String quality = line.substring(92, 93);  
    19.     if (airTemperature != MISSING && quality.matches("[01459]")) {  
    20.       context.write(new Text(year), new IntWritable(airTemperature));         
    21.     }       
    22.   }     
    23. }     
    24.    
    25.   static class NewMaxTemperatureReducer  
    26.     extends Reducer<Text, IntWritable, Text, IntWritable> {         
    27.     public void reduce(Text key, Iterable<IntWritable> values,           
    28.         Context context)           
    29.         throws IOException, InterruptedException {               
    30.  
    31.       int maxValue = Integer.MIN_VALUE;         
    32.       for (IntWritable value : values) {           
    33.         maxValue = Math.max(maxValue, value.get());         
    34.       }         
    35.       context.write(key, new IntWritable(maxValue));       
    36.     }     
    37.   }  
    38.  
    39. public static void main(String[] args) throws Exception {       
    40.   if (args.length != 2) {         
    41.     System.err.println("Usage: NewMaxTemperature
      <input path> <output path>");     
    42.     System.exit(-1);  
    43.     }           
    44.  
    45.     Job job = new Job();       
    46.     job.setJarByClass(NewMaxTemperature.class);  
    47.  
    48.     FileInputFormat.addInputPath(job, new Path(args[0]));  
    49.     FileOutputFormat.setOutputPath(job, new Path(args[1]));           
    50.  
    51.     job.setMapperClass(NewMaxTemperatureMapper.class);       
    52.     job.setReducerClass(NewMaxTemperatureReducer.class);  
    53.     job.setOutputKeyClass(Text.class);  
    54.     job.setOutputValueClass(IntWritable.class);           
    55.  
    56.     System.exit(job.waitForCompletion(true) ? 0 : 1);     
    57.   }  
    58. }  
  • 相关阅读:
    Linux学习笔记六----------文件传输
    Linux学习笔记五----------文本编辑
    Linux学习笔记四----------远程连接和SSH
    Linux学习笔记三----------Linux进阶知识和命令
    Linux学习笔记二----------Linux基础知识和命令
    ArcGIS API for JavaScript3.16 使用中遇到的问题,及解决方法
    ArcGIS API for JavaScript学习
    echarts容器动态变化高度
    好用的流程图js插件
    jeecg-boot + ant-design-vue开发,希望点击菜单打开新窗口页签
  • 原文地址:https://www.cnblogs.com/mrcharles/p/11879856.html
Copyright © 2020-2023  润新知