Hadoop MapReduce新旧API区别
来源:互联网 发布:托福网络培训班 编辑:程序博客网 时间:2024/05/21 19:31
新增的Java MapReduce API
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应用
public class NewMaxTemperature { static class NewMaxTemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private static final int MISSING = 9999; public void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException { String line = value.toString(); String year = line.substring(15, 19); int airTemperature; if (line.charAt(87) == '+') { // parseInt doesn't like leading plus signs airTemperature = Integer.parseInt(line.substring(88, 92)); } else { airTemperature = Integer.parseInt(line.substring(87, 92)); } String quality = line.substring(92, 93); if (airTemperature != MISSING && quality.matches("[01459]")) { context.write(new Text(year), new IntWritable(airTemperature)); } } } static class NewMaxTemperatureReducer extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int maxValue = Integer.MIN_VALUE; for (IntWritable value : values) { maxValue = Math.max(maxValue, value.get()); } context.write(key, new IntWritable(maxValue)); } } public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: NewMaxTemperature<input path> <output path>"); System.exit(-1); } Job job = new Job(); job.setJarByClass(NewMaxTemperature.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setMapperClass(NewMaxTemperatureMapper.class); job.setReducerClass(NewMaxTemperatureReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
原来MapReduce代码可在《Hadoop权威指南》内找到,大家可进行对比。
又一例子:Hadoop in Action中第四章:
package com;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.conf.Configured;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.LongWritable;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.input.TextInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner;public class tt extends Configured implements Tool {public static class MapClass extends Mapper<LongWritable, Text, Text, Text> {public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException {//split的作用是将该字符串里面的变量赋值给citation这个字符串数组当中。String[] citation = value.toString().split(",");//使用新的API取代了collect相关的API,将map中的key和value进行了互换。context.write(new Text(citation[1]), new Text(citation[0])); }}public static class Reduce extends Reducer<Text, Text, Text, Text> { //前两个参数设置是输入参数,后两个参数是输出参数。public void reduce(Text key, Iterable<Text> values, Context context)throws IOException, InterruptedException {String csv ="";//Text类型是类似于String类型的文本格式,但是在处理编码上还是和String有差别,与内存序列化有关,是hadoop经过封装之后的新类。for (Text val:values) {if (csv.length() > 0) csv += ",";csv += val.toString();}context.write(key, new Text(csv));}}public int run(String[] args) throws Exception { //由hadoop本身调用该程序Configuration conf = getConf();Job job = new Job(conf, "tt"); //利用job取代了jobclientjob.setJarByClass(tt.class);Path in = new Path(args[0]);Path out = new Path(args[1]);FileInputFormat.setInputPaths(job, in);FileOutputFormat.setOutputPath(job, out);job.setMapperClass(MapClass.class);job.setReducerClass(Reduce.class);job.setInputFormatClass(TextInputFormat.class);job.setOutputFormatClass(TextOutputFormat.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class); //此处如果不进行设置,系统会抛出异常,还要记住新旧API不能混用System.exit(job.waitForCompletion(true)?0:1);return 0;}public static void main(String[] args) throws Exception {int res = ToolRunner.run(new Configuration(), new tt(), args); //调用新的类的方法免除配置的相关琐碎的细节System.exit(res);}}
- Hadoop MapReduce新旧API区别
- mapreduce 新旧API 区别
- mapreduce 新旧API 区别
- Hadoop 新旧MapReduce API比较
- Hadoop新旧API对比初探+链式mapreduce
- 【Hadoop】新旧Java MapReduce API的差异
- [hadoop源码阅读][3]-新旧api区别
- Hadoop新旧API的区别和联系
- Hadoop新旧API区别2<转>
- MapReduce 新旧API
- hadoop的新旧api
- Hadoop 新旧API对比
- Hadoop学习笔记二(新旧版本API的区别)
- MR新旧API区别
- 新旧 Hadoop MapReduce 框架比对
- 新旧 Hadoop MapReduce 框架比对
- Java MapReduce新旧API的比较
- Hadoop实践(三)---新旧API对比
- 函数模板和类模板的实例化和具体化 - 2
- 一句经典的传入字符串在存储过程中的处理办法
- Android的四大组件之三--Activity(4)----->Activity的启动方式和相关参数
- Eclipse V3.5.2 | java 开发工具
- MySQL 5.1.60
- Hadoop MapReduce新旧API区别
- 黑马程序员--C#WinForm学习笔记
- 标准类库 queue模板类的简单实现
- java类加载的思考
- Windows7建立隐藏账户
- 测试架构支撑商业成功(第二部分
- 仿的一个动画菜单效果
- 深入理解Android 之界面构造
- ctags配置笔记