将输出文件添加到Mapreduce中的现有输出目录

时间:2014-09-02 11:50:53

标签: hadoop mapreduce

我希望每次运行作业时都将map reduce程序的输出文件添加到同一目录,方法是在文件名末尾添加时间戳。

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

2 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分:0)

可以使用reduce方法控制输出。我想你可以在reducer中尝试一个逻辑。

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