我有一个MapReduce
个应用程序,其中包含2个作业。
我需要在 Job1 和 Job2 之间运行一个小代码。换句话说,在 Job1 的最终输出上运行一个小代码, Job2 中的所有映射器都能够使用这个小代码的输出。
这个小代码不需要并行运行。它是一个顺序代码,应该在一台机器上运行并在HDFS中写入输出。
我想知道如何在 Job1 和 Job2 之间的应用程序代码中编写顺序代码,该代码将在一台机器上运行并读取的输出来自HDFS的Job1 并在HDFS中编写自己的输出。
答案 0 :(得分:0)
在您的Driver(main)类中,执行 Job1 之后(通常是JobClient.runJob(conf);
命令)并且在设置 Job2 的第一个命令之前,使用hadoop的FileSystem
获取 Job1 的输出并设置 Job2 的输入,如下所示:
//...run job1
BufferedWriter bw = null;
BufferedReader br = null;
try {
String job1outPath = "/job1/output/";
Path job2in = new Path("/job2/in/oneFile.txt");
bw = new BufferedWriter(new OutputStreamWriter(fs.create(job2in,true)));
FileSystem fs = FileSystem.get(new Configuration());
FileStatus[] status = fs.listStatus(new Path(job1outPath));
for (int i=0;i<status.length;i++){
br=new BufferedReader(new InputStreamReader(fs.open(status[i].getPath())));
String line;
while ((line = br.readLine())!= null){
bw.write("whatever"); //process the output of job1 as you wish
bw.newLine();
}
}
} catch (IOException e) {
System.err.println(e);
} finally {
try{
bw.close();
br.close(); //not sure if you need this inside the for loop
} catch (IOException io) {
System.out.println(io);
}
}
//optionally delete "/job1/output/*" here
//...run job2 using "/job2/in" as your input path
这将在主节点中按顺序运行。如果愿意,您还可以使用System.out.println()
命令查看终端中的有用信息。
希望它编译,我没有通过编辑器传递它,但我相信你可以让它工作。