Amaresh Narayanan Amaresh Narayanan - 1 year ago 54
Java Question

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

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.

Answer Source

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();
                }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download