1

HDFS でファイルを開いて書き込みを担当するオブジェクトがあります。close()このオブジェクトは、メソッドが呼び出されると、書き込んだばかりのファイルの名前を変更します。このメカニズムは、ローカル モードで実行すると機能しますが、クラスター モードでファイルの名前を変更することはできません。

//Constructor
public WriteStream() {
    path = String.format("in_progress/file");
    try {
        OutputStream outputStream = fileSystem.create(new Path(hdfs_path+path), new Progressable() {public void progress() { System.out.print("."); }
            });
        writer = new BufferedWriter(new OutputStreamWriter(outputStream));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void close() {
    String newPath = String.format("%s_dir/%s_file", date, timestamp);
    try {
        fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

以前にそれを経験しましたか?

4

2 に答える 2

1

ちょっと興味がありますが、正式には存在しないファイルの名前を変更するにはどうすればよいでしょうか (その時点でまだ書いているため)。

修正は、ファイルが完成した後に名前を変更することです。つまり、close メソッドを呼び出したときです。

したがって、コードは次のようになります。

public void close() {
    String newPath = String.format("%s_dir/%s_file", date, timestamp);
    try {
        writer.close();
        fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
于 2013-06-27T09:30:50.877 に答える