2014-09-02 47 views
2

我希望在每次通过在文件名末尾追加时间戳记来运行作业时,将地图缩减程序的输出文件添加到同一目录。将输出文件添加到Mapreduce中的现有输出目录

目前我能够在文件输出文件的末尾添加时间戳,但我无法找到如何将文件添加到同一个输出目录而不是每次覆盖它。

回答

2

您可以将输出文件写入临时文件夹,并在作业结束后将其移至目标文件夹。从一个文件夹移动所有文件到另一个方法的例子:

public static void moveFiles(Path from, Path to, Configuration conf) throws IOException { 
    FileSystem fs = from.getFileSystem(conf); // get file system 
    for (FileStatus status : fs.listStatus(from)) { // list all files in 'from' folder 
     Path file = status.getPath(); // get path to file in 'from' folder 
     Path dst = new Path(to, file.getName()); // create new file name 
     fs.rename(file, dst); // move file from 'from' folder to 'to' folder 
    } 
} 
+0

目前我下面什么都你提到使用Fileutil.copy相同的逻辑,但问题是,它正在改变时间戳以前生成的文件也是因为复制,我想保留以前生成的文件的时间戳。 – 2014-09-02 13:30:43

+0

据我所知,你只需将新文件复制到目标文件夹即可。为什么它会更改以前生成的文件的时间戳? – 2014-09-02 14:27:07

+0

Fileutil.copy将文件从源移到目标,因为哪个时间戳正在改变。现在我已将其更改为fs.rename。它不会更改时间戳。谢谢.. – 2014-09-03 06:34:22

0

输出可以使用减法来控制。我想你可以尝试减速器中的逻辑。

请注意reducer的数量=输出文件的数量。