MapReduce多重MR如何实现

时间:2023-03-10 03:27:17
MapReduce多重MR如何实现

一、每次输出文件存在很烦人

// 判断output文件夹是否存在,如果存在则删除
Path path = new Path(otherArgs[1]);// 取第1个表示输出目录参数(第0个参数是输入目录)
FileSystem fileSystem = path.getFileSystem(conf);// 根据path找到这个文件
if (fileSystem.exists(path)) {
fileSystem.delete(path, true);// true的意思是,就算output有东西,也一带删除
}

二、多重MR

    /设置第一轮MapReduce的相应处理类与输入输出
Job job1 = new Job(conf);
.
.
. // 定义一个临时目录,先将任务的输出结果写到临时目录中, 下一个job以临时目录为输入目录。
FileInputFormat.addInputPath(job1, new Path(otherArgs[0]));
Path tempDir = new Path("temp_"
+ Integer.toString(new Random().nextInt(Integer.MAX_VALUE)));
FileOutputFormat.setOutputPath(job1, tempDir); if (job1.waitForCompletion(true)) {//如果第一轮MapReduce完成再做这里的代码
Job job2 = new Job(conf);
FileInputFormat.addInputPath(job2, tempDir);
//设置第二轮MapReduce的相应处理类与输入输出
.
.
FileOutputFormat.setOutputPath(job2, new Path(otherArgs[1])); FileSystem.get(conf).deleteOnExit(tempDir);//搞完删除刚刚的临时创建的输入目录
System.exit(job2.waitForCompletion(true) ? 0 : 1);
}