Example usage for org.apache.hadoop.fs FileUtil stat2Paths

List of usage examples for org.apache.hadoop.fs FileUtil stat2Paths

Introduction

In this page you can find the example usage for org.apache.hadoop.fs FileUtil stat2Paths.

Prototype

public static Path[] stat2Paths(FileStatus[] stats, Path path) 

Source Link

Document

convert an array of FileStatus to an array of Path.

Usage

From source file:org.wso2.carbon.hdfs.mgt.HDFSAdmin.java

License:Open Source License

/**
 * Copy  a given path to another path.//from w w  w .j  ava2s  .  com
 * @param srcPath the src path to copy.
 * @param dstPath the destination to copy to.
 * @throws HDFSServerManagementException
 */
public void copy(String srcPath, String dstPath) throws HDFSServerManagementException {

    FileSystem hdfsFS = null;
    try {
        hdfsFS = hdfsAdminHelperInstance.getFSforUser();
    } catch (IOException e) {
        String msg = "Error occurred while mouting the file system";
        handleException(msg, e);
    }

    Path[] srcs = new Path[0];
    if (hdfsFS != null) {
        try {
            srcs = FileUtil.stat2Paths(hdfsFS.globStatus(new Path(srcPath)), new Path(srcPath));
        } catch (IOException e) {
            String msg = "Error occurred while trying to copy file.";
            handleException(msg, e);
        }
    }
    try {
        if (srcs.length > 1 && !hdfsFS.getFileStatus(new Path(dstPath)).isDir()) {
            throw new IOException("When copying multiple files, " + "destination should be a directory.");
        }
    } catch (IOException e) {
        String msg = "Error occurred while trying to copy file.";
        handleException(msg, e);
    }
    Configuration configuration = new Configuration();
    configuration.set("io.file.buffer.size", Integer.toString(4096));
    for (int i = 0; i < srcs.length; i++) {
        try {
            FileUtil.copy(hdfsFS, srcs[i], hdfsFS, new Path(dstPath), false, configuration);
        } catch (IOException e) {
            String msg = "Error occurred while trying to copy file.";
            handleException(msg, e);
        }
    }
}

From source file:simsql.code_generator.MyPhysicalDatabase.java

License:Apache License

public void backupTo(String toHere) {

    ConsoleReader cr = null;// w ww .  j  av a  2  s  .  c  o m
    try {
        cr = new ConsoleReader();
    } catch (Exception e) {
        throw new RuntimeException("Could not create a console for reading!", e);
    }

    try {

        // first, we see if the directory that we are writing to exists
        Configuration conf = new Configuration();
        FileSystem dfs = FileSystem.get(conf);

        Path pathTo = new Path(toHere);
        if (dfs.exists(pathTo)) {
            System.out.format("The specified path already exists. Would you like to overwrite it? [Y/N] ");

            char answer = (char) cr.readCharacter(new char[] { 'Y', 'N', 'y', 'n' });
            System.out.println(answer);

            if (answer == 'Y' || answer == 'y') {
                dfs.delete(pathTo, true);
            } else {

                // otherwise, we don't proceed.
                pathTo = null;
            }
        }

        // do we continue?
        if (pathTo != null) {

            // make it a directory
            // dfs.mkdirs(pathTo);

            // get our current directory.
            Path pathFrom = new Path(myDir);

            // and all the paths of the in our directory
            Path[] sourcePaths = FileUtil.stat2Paths(dfs.globStatus(pathFrom), pathFrom);
            for (Path sp : sourcePaths) {

                // copy all of it.
                FileUtil.copy(dfs, sp, dfs, pathTo, false, conf);
            }
        }

    } catch (Exception e) {
        throw new RuntimeException("Could not back up data into directory " + toHere, e);
    }
}

From source file:simsql.code_generator.MyPhysicalDatabase.java

License:Apache License

public void restoreFrom(String fromHere) {

    try {/*from   ww  w.ja v a  2s.c o  m*/

        // first, we see if the directory that we are reading from
        // exists and is a directory.
        Configuration conf = new Configuration();
        FileSystem dfs = FileSystem.get(conf);

        Path pathFrom = new Path(fromHere);
        if (!dfs.exists(pathFrom) || !dfs.isDirectory(pathFrom)) {
            System.out.println("The specified restoration path does not exist or is not a directory!");
            return;
        }

        // now, get the destination path.
        Path pathTo = new Path(myDir);
        if (dfs.exists(pathTo)) {

            // destroy it, if it's there.
            dfs.delete(pathTo, true);
        }

        // make the directory
        // dfs.mkdirs(pathTo);

        // and all the paths we will be copying
        Path[] sourcePaths = FileUtil.stat2Paths(dfs.globStatus(pathFrom), pathFrom);
        for (Path sp : sourcePaths) {

            // restore all of it.
            FileUtil.copy(dfs, sp, dfs, pathTo, false, conf);
        }

    } catch (Exception e) {
        throw new RuntimeException("Could not restore data from directory " + fromHere, e);
    }
}