Example usage for org.apache.hadoop.fs LocalFileSystem pathToFile

List of usage examples for org.apache.hadoop.fs LocalFileSystem pathToFile

Introduction

In this page you can find the example usage for org.apache.hadoop.fs LocalFileSystem pathToFile.

Prototype

public File pathToFile(Path path) 

Source Link

Document

Convert a path to a File.

Usage

From source file:gobblin.util.HadoopUtils.java

License:Apache License

/**
 * Renames a src {@link Path} on fs {@link FileSystem} to a dst {@link Path}. If fs is a {@link LocalFileSystem} and
 * src is a directory then {@link File#renameTo} is called directly to avoid a directory rename race condition where
 * {@link org.apache.hadoop.fs.RawLocalFileSystem#rename} copies the conflicting src directory into dst resulting in
 * an extra nested level, such as /root/a/b/c/e/e where e is repeated.
 *
 * @param fs the {@link FileSystem} where the src {@link Path} exists
 * @param src the source {@link Path} which will be renamed
 * @param dst the {@link Path} to rename to
 * @return true if rename succeeded, false if rename failed.
 * @throws IOException if rename failed for reasons other than target exists.
 *//*w w w .j a va2 s  . c  om*/
public static boolean renamePathHandleLocalFSRace(FileSystem fs, Path src, Path dst) throws IOException {
    if (DecoratorUtils.resolveUnderlyingObject(fs) instanceof LocalFileSystem && fs.isDirectory(src)) {
        LocalFileSystem localFs = (LocalFileSystem) DecoratorUtils.resolveUnderlyingObject(fs);
        File srcFile = localFs.pathToFile(src);
        File dstFile = localFs.pathToFile(dst);

        return srcFile.renameTo(dstFile);
    } else {
        return fs.rename(src, dst);
    }
}