Example usage for org.apache.hadoop.fs Trash Trash

List of usage examples for org.apache.hadoop.fs Trash Trash

Introduction

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

Prototype

public Trash(FileSystem fs, Configuration conf) throws IOException 

Source Link

Document

Construct a trash can accessor for the FileSystem provided.

Usage

From source file:com.revolutionanalytics.hadoop.hdfs.FileUtils.java

License:Apache License

private static void delete(Configuration cfg, FileSystem srcFS, Path src, boolean recursive)
        throws IOException {
    Trash trashTmp = new Trash(srcFS, cfg);
    if (trashTmp.moveToTrash(src)) {
        System.out.println("Moved to trash: " + src);
        return;/*from   w  w w.  j  a  va2 s  .com*/
    }
    if (srcFS.delete(src, true)) {
        System.out.println("Deleted " + src);
    } else {
        if (!srcFS.exists(src)) {
            throw new FileNotFoundException("cannot remove " + src + ": No such file or directory.");
        }
        throw new IOException("Delete failed " + src);
    }
}

From source file:org.apache.accumulo.server.fs.VolumeManagerImpl.java

License:Apache License

@Override
public boolean moveToTrash(Path path) throws IOException {
    FileSystem fs = getVolumeByPath(path).getFileSystem();
    Trash trash = new Trash(fs, fs.getConf());
    return trash.moveToTrash(path);
}

From source file:org.apache.ambari.view.filebrowser.HdfsApi.java

License:Apache License

/**
 * Check is trash enabled//  w w  w .  java 2s  .c  o m
 * @return true if trash is enabled
 * @throws Exception
 */
public boolean trashEnabled() throws Exception {
    return ugi.doAs(new PrivilegedExceptionAction<Boolean>() {
        public Boolean run() throws IOException {
            Trash tr = new Trash(fs, conf);
            return tr.isEnabled();
        }
    });
}

From source file:org.apache.ambari.view.filebrowser.HdfsApi.java

License:Apache License

/**
 * Empty trash/*  w ww . j  a va  2s. c  o m*/
 * @return
 * @throws Exception
 */
public Void emptyTrash() throws Exception {
    return ugi.doAs(new PrivilegedExceptionAction<Void>() {
        public Void run() throws IOException {
            Trash tr = new Trash(fs, conf);
            tr.expunge();
            return null;
        }
    });
}

From source file:org.apache.ambari.view.utils.hdfs.HdfsApi.java

License:Apache License

/**
 * Check is trash enabled/*from w ww .  j  a v  a 2  s  . c o  m*/
 * @return true if trash is enabled
 * @throws Exception
 */
public boolean trashEnabled() throws Exception {
    return execute(new PrivilegedExceptionAction<Boolean>() {
        public Boolean run() throws IOException {
            Trash tr = new Trash(fs, conf);
            return tr.isEnabled();
        }
    });
}

From source file:org.apache.ambari.view.utils.hdfs.HdfsApi.java

License:Apache License

/**
 * Empty trash//from  w  w w  . j a v a  2 s.  c om
 * @return
 * @throws Exception
 */
public Void emptyTrash() throws Exception {
    return execute(new PrivilegedExceptionAction<Void>() {
        public Void run() throws IOException {
            Trash tr = new Trash(fs, conf);
            tr.expunge();
            return null;
        }
    });
}

From source file:org.apache.gobblin.util.HadoopUtils.java

License:Apache License

/**
 * Moves the object to the filesystem trash according to the file system policy.
 * @param fs FileSystem object//w  w  w.  j  a  v  a  2  s  .  co  m
 * @param path Path to the object to be moved to trash.
 * @throws IOException
 */
public static void moveToTrash(FileSystem fs, Path path) throws IOException {
    Trash trash = new Trash(fs, new Configuration());
    trash.moveToTrash(path);
}

From source file:org.apache.gobblin.util.HadoopUtilsTest.java

License:Apache License

@Test
public void testMoveToTrash() throws IOException {
    Path hadoopUtilsTestDir = new Path(Files.createTempDir().getAbsolutePath(), "HadoopUtilsTestDir");
    Configuration conf = new Configuration();
    // Set the time to keep it in trash to 10 minutes.
    // 0 means object will be deleted instantly.
    conf.set("fs.trash.interval", "10");
    FileSystem fs = FileSystem.getLocal(conf);
    Trash trash = new Trash(fs, conf);
    TrashPolicy trashPolicy = TrashPolicy.getInstance(conf, fs, fs.getHomeDirectory());
    Path trashPath = trashPolicy.getCurrentTrashDir();

    fs.mkdirs(hadoopUtilsTestDir);//from   www  . j  ava  2s. c  om
    Assert.assertTrue(fs.exists(hadoopUtilsTestDir));
    trash.moveToTrash(hadoopUtilsTestDir.getParent());
    Assert.assertFalse(fs.exists(hadoopUtilsTestDir));
    Assert.assertTrue(fs.exists(trashPath));
}

From source file:org.godhuli.rhipe.FileUtils.java

License:Apache License

private void delete(Path src, FileSystem srcFs, boolean recursive) throws IOException {
    if (srcFs.isDirectory(src) && !recursive) {
        throw new IOException("Cannot remove directory \"" + src + "\", use -rmr instead");
    }/* w w  w . j  a v  a2  s .  c o  m*/
    Trash trashTmp = new Trash(srcFs, getConf());
    if (trashTmp.moveToTrash(src)) {
        System.out.println("Moved to trash: " + src);
        return;
    }
    if (srcFs.delete(src, true)) {
        System.out.println("Deleted " + src);
    } else {
        if (!srcFs.exists(src)) {
            throw new FileNotFoundException("cannot remove " + src + ": No such file or directory.");
        }
        throw new IOException("Delete failed " + src);
    }
}

From source file:org.springframework.data.hadoop.fs.FsShell.java

License:Apache License

public void rm(boolean recursive, boolean skipTrash, String... uris) {
    for (String uri : uris) {
        try {/* w  ww.jav  a 2  s.com*/
            Path src = new Path(uri);
            FileSystem srcFs = getFS(src);

            for (Path p : FileUtil.stat2Paths(srcFs.globStatus(src), src)) {
                FileStatus status = srcFs.getFileStatus(p);
                if (status.isDir() && !recursive) {
                    throw new IllegalStateException(
                            "Cannot remove directory \"" + src + "\", if recursive deletion was not specified");
                }
                if (!skipTrash) {
                    try {
                        Trash trashTmp = new Trash(srcFs, configuration);
                        trashTmp.moveToTrash(p);
                    } catch (IOException ex) {
                        throw new HadoopException("Cannot move to Trash resource " + p, ex);
                    }
                }
                srcFs.delete(p, recursive);
            }
        } catch (IOException ex) {
            throw new HadoopException("Cannot delete (all) resources " + ex.getMessage(), ex);
        }
    }
}