影响大多数MapReduce程序效率的主要瓶颈是磁盘和网络I/O。
使用Mapper去执行部分计算,可以有效减少Mapper和Reducer之间的I/O开销。相较于将全部输入传输给Reducer,并执行计算任务,这样的开销是非常小的,在MapReduce开发中,需要考虑这些问题,以降低开销,提高效率。
1、在MapReduce中输出时,如何不输出Key值?
NullWritable.get()方法
当Mapper和Reducer的输出类型相同的时候,只需要设置这2个就可以了:
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(Text.class);
当Mapper和Reducer输出类型不同时,需要分别设置:
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(Text.class);
2、GenericOptionsParser的用法
使用GenericOptionsParser将命令参数传递给作业:
- 使用-libjars
<comma separated jar paths(逗号分隔的jar路径)>传递第三方库 - 使用-D
<name> = <value>传递自定义的命令参数
new GenericOptionsParser(getConf(),args).getRemainingArgs();
在本例中,会返回包含输入输出路径的数组
3、实现Tool接口
Tool接口支持处理通用命令行选项
Tool是任何Map-Reduce工具/应用程序的标准。 工具/应用程序应该将标准命令行选项的处理委托给ToolRunner.run(Tool,String []),并且仅处理其自定义参数。
以下是典型工具的实现方式:
public class MyApp extends Configured implements Tool {
public int run(String[] args) throws Exception {
// Configuration processed by ToolRunner
Configuration conf = getConf();
// Create a JobConf using the processed conf
JobConf job = new JobConf(conf, MyApp.class);
// Process custom command-line options
Path in = new Path(args[1]);
Path out = new Path(args[2]);
// Specify various job-specific parameters
job.setJobName("my-app");
job.setInputPath(in);
job.setOutputPath(out);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
// Submit the job, then poll for progress until the job is complete
JobClient.runJob(job);
return 0;
}
public static void main(String[] args) throws Exception {
// Let ToolRunner handle generic command-line options
int res = ToolRunner.run(new Configuration(), new MyApp(), args);
System.exit(res);
}
}

本文探讨了MapReduce程序效率的关键因素——磁盘和网络I/O,并分享了三个实用技巧:1) 如何在MapReduce中省略Key输出以减少I/O;2) 使用GenericOptionsParser处理命令行参数,包括传递第三方库和自定义参数;3) 实现Tool接口,优化标准命令行选项处理。这些策略有助于降低开销,提高MapReduce作业的效率。
---MapReduce编程 小技巧&spm=1001.2101.3001.5002&articleId=52653041&d=1&t=3&u=4c1d58bf5f2c4bd5b924bab194b68aa2)
3396

被折叠的 条评论
为什么被折叠?



