您的位置:首页 > 编程语言

Hadoop实践(三)---MapReduce编程 小技巧

2016-09-24 19:57 323 查看
影响大多数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 编程 Hadoop