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

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

Introduction

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

Prototype

public boolean moveToTrash(Path path) throws IOException 

Source Link

Document

Move a file or directory to the current trash directory.

Usage

From source file:org.springframework.xd.shell.hadoop.FsShellCommands.java

License:Apache License

@CliCommand(value = PREFIX + "rm", help = "Remove files in the HDFS")
public void rm(@CliOption(key = { "",
        PATH }, mandatory = false, unspecifiedDefaultValue = ".", help = "path to be deleted") final String path,
        @CliOption(key = {//from   w  w  w .j  ava2  s.  c  o m
                "skipTrash" }, mandatory = false, specifiedDefaultValue = TRUE, unspecifiedDefaultValue = FALSE, help = "whether to skip trash") final boolean skipTrash,
        @CliOption(key = {
                RECURSIVE }, mandatory = false, specifiedDefaultValue = TRUE, unspecifiedDefaultValue = FALSE, help = "whether to recurse") final boolean recursive) {
    try {
        Path file = new Path(path);
        FileSystem fs = file.getFileSystem(getHadoopConfiguration());
        for (Path p : FileUtil.stat2Paths(fs.globStatus(file), file)) {
            FileStatus status = fs.getFileStatus(p);
            if (status.isDirectory() && !recursive) {
                LOG.error("To remove directory, please use 'fs rm </path/to/dir> --recursive' instead");
                return;
            }
            if (!skipTrash) {
                Trash trash = new Trash(fs, getHadoopConfiguration());
                trash.moveToTrash(p);
            }
            fs.delete(p, recursive);
        }
    } catch (Exception t) {
        LOG.error("Exception: run HDFS shell failed. Message is: " + t.getMessage());
    } catch (Error t) {
        LOG.error("Error: run HDFS shell failed. Message is: " + t.getMessage());
    }
}