Hadoop:如何将reducer输出合并到一个文件?

我知道shell中的“getmerge”命令可以完成这项工作。

但是,如果我想在作业之后通过HDFS API for java合并这些输出,我该怎么办?

我真正想要的是HDFS上的单个合并文件。

我唯一能想到的就是在那之后开始一项额外的工作。

谢谢!

但是,如果我想在作业之后通过HDFS API for java合并这些输出,我该怎么办?

猜测,因为我自己没有尝试过,但我认为您正在寻找的方法是FileUtil.copyMerge,这是FsShell在运行-getmerge命令时调用的方法。 FileUtil.copyMerge将两个FileSystem对象作为参数–FsShell使用FileSystem.getLocal来检索目标FileSystem,但我没有看到任何理由您不能在目标上使用Path.getFileSystem来获取OutputStream

也就是说,我不认为它会赢得你很多 – 合并仍在本地JVM中发生; 所以你并没有真正节省超过-getmerge后跟-put

通过在代码中设置单个Reducer,可以获得单个输出文件。

 Job.setNumberOfReducer(1); 

将满足您的要求,但成本高昂


要么


 Static method to execute a shell command. Covers most of the simple cases without requiring the user to implement the Shell interface. Parameters: env the map of environment key=value cmd shell command to execute. Returns: the output of the executed command. 

 org.apache.hadoop.util.Shell.execCommand(String[])