Is there any way to check whether the Hadoop file is already opened for write?

你。 提交于 2020-01-06 07:58:49

问题


Multiple Java instances are running on my machine and I want to check whether the Hadoop file is already opened in write (fs.create(file) or fs.append(file)) mode in any of the instances.

I Tried in FileStatus of the Hadoop file, not found anything.

Is there any way to check whether the Hadoop file is already opened for write?

One way is to try to create/append a file again and catch the exception, but I have thousands of files and don't want to try every file. Also, if create/append is a success, then I have to close the file and lastModifiedTime will also get changed. I don't want to modify the FileStatus of a Hadoop file.


回答1:


DistributedFileSystem provides the method isFileClosed(path) to check whether the file is opened for write.

try {
    DistributedFileSystem dfs = new DistributedFileSystem();
    Path path = new Path("/path/to/hadoop/file");
    FileSystem fs = path.getFileSystem(conf);
    dfs.initialize(fs.getUri(),conf);

    if (dfs.exists(path) && !dfs.isFileClosed(path)) {
        System.out.println("File " + path +" already opened in write mode");
    }
} catch (IOException e) {
    e.printStackTrace();
}


来源:https://stackoverflow.com/questions/45605977/is-there-any-way-to-check-whether-the-hadoop-file-is-already-opened-for-write

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!