问题
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