在开发MapReduce程序的过程中,可以首先在本地文件系统上对程序进行测试,而不是一开始就在HDFS上,这样调试起来更加方便。
以《Hadoop权威指南》上的MaxTemperature程序为例,整个项目中包括如下3个源文件,依次为Mapper程序、Reducer程序和job启动程序:
MaxTemperatureMapper.java,MaxTemperatureReducer.java,MaxTemperatureDriver.java
MaxTemperatureMapper.java和MaxTemperatureReducer.java的代码可在MRUnit的安装和使用中找到。
MaxTemperatureDriver.java
import org.apache.hadoop.conf.Configured; 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.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; public class MaxTemperatureDriver extends Configured implements Tool { public int run(String[] args) throws Exception { if(args.length != 2) { System.err.printf("Usage: %s [generic options] <input> <output>\n", getClass().getSimpleName()); ToolRunner.printGenericCommandUsage(System.err); return -1; } Job job = new Job(getConf(), "Max temperature"); job.setJarByClass(getClass()); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setMapperClass(MaxTemperatureMapper.class); job.setCombinerClass(MaxTemperatureReducer.class); job.setReducerClass(MaxTemperatureReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); return job.waitForCompletion(true) ? 0 : 1; } public static void main(String[] args) throws Exception { int exitCode = ToolRunner.run(new MaxTemperatureDriver(), args); System.exit(exitCode); } }
选中文件MaxTemperatureDriver.java-->右键Run As-->Run Configurations-->New launch configuration-->选中Arguments,在Program arguments中填入本地文件系统中的输入输出路径,例如
file:///home/hadoop/input file:///home/hadoop/output点击Apply-->点击Run。
执行结束后,可以根据输出文件的内容判断程序是否正确。
原文地址:http://blog.csdn.net/warren912/article/details/42874449