• HDFS 的Trash回收站


    1)在core-site.xml文件中添加这个配置

    在每个节点(不仅仅是主节点)上添加配置 core-site.xml,增加如下内容
    <property>
        <name>fs.trash.interval</name>
        <value>1440</value>
    </property>

    1440表示在文件放入回收站1440分钟之后才会真得彻底的被删除

    2)使用命令行删除文件:

    //查看HDFS下的根目录
    root@Ubuntu-1:/usr/local/hadoop-2.6.0/bin# hadoop fs -ls /
    Found 1 items
    drwxr-xr-x   - root supergroup          0 2017-05-19 11:02 /test
    //删除文件
    root@Ubuntu-1:/usr/local/hadoop-2.6.0/bin# hadoop fs -rm -r /test
    17/05/22 15:39:00 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 1440 minutes, Emptier interval = 0 minutes.
    Moved: 'hdfs://Ubuntu-1:9000/test' to trash at: hdfs://Ubuntu-1:9000/user/root/.Trash/Current
    //查看回收站的文件:

    root@Ubuntu-1:/usr/local/hadoop-2.6.0# bin/hdfs dfs -ls hdfs://Ubuntu-1:9000/user/root/.Trash/Current
    Found 1 items
    drwxr-xr-x - root supergroup 0 2017-05-19 11:02 hdfs://Ubuntu-1:9000/user/root/.Trash/Current/test

    //撤回回收站的文件
    root@Ubuntu-1:/usr/local/hadoop-2.6.0# bin/hadoop fs -mv /user/root/.Trash/Current /user/root/test
    //文件已撤回
    root@Ubuntu-1:/usr/local/hadoop-2.6.0# bin/hadoop fs -ls
    Found 2 items
    drwx------   - root supergroup          0 2017-05-22 16:12 .Trash
    drwx------   - root supergroup          0 2017-05-22 15:39 test

    3)使用java代码操作HDFS的回收站

    import java.io.IOException;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.fs.Trash;
    
    public class RMFile {
        private final static Log log = LogFactory.getLog(RMFile.class);
        private final static Configuration conf = new Configuration();
    
        /**
         * Delete a file/directory on hdfs
         * 
         * @param path
         * @param recursive
         * @return
         * @throws IOException
         */
        public static boolean rm(FileSystem fs, Path path, boolean recursive)
                throws IOException {
            log.info("rm: " + path + " recursive: " + recursive);
            boolean ret = fs.delete(path, recursive);
            if (ret)
                log.info("rm: " + path);
            return ret;
    
        }
    
        /**
         * Delete a file/directory on hdfs,and move a file/directory to Trash
         * @param fs
         * @param path
         * @param recursive
         * @param skipTrash
         * @return
         * @throws IOException
         */
        public static boolean rm(FileSystem fs, Path path, boolean recursive,
                boolean skipTrash) throws IOException {
            log.info("rm: " + path + " recursive: " + recursive+" skipTrash:"+skipTrash);
            if (!skipTrash) {
                Trash trashTmp = new Trash(fs, conf);
                if (trashTmp.moveToTrash(path)) {
                    log.info("Moved to trash: " + path);
                    return true;
                }
            }
            boolean ret = fs.delete(path, recursive);
            if (ret)
                log.info("rm: " + path);
            return ret;
    
        }
    
        public static void main(String[] args) throws IOException {
            conf.set("fs.default.name", "hdfs://data2.kt:8020/");
            FileSystem fs = FileSystem.get(conf);
            RMFile.rm(fs,new Path("hdfs://data2.kt:8020/test/testrm"),true,false);
        }
    
    } 
  • 相关阅读:
    常用模板
    pascal 的字符串操作
    war2 洛谷模拟赛day2 t3 状压
    状压搜索 洛谷T47092 作业
    Milking Order
    洛谷九月月赛T1 思考
    C数列下标 牛客OI赛制测试赛2
    钓鱼 洛谷p1717
    机房人民大团结(DP)
    Spark的Straggler深入学习(2):思考Block和Partition的划分问题——以论文为参考
  • 原文地址:https://www.cnblogs.com/huxinga/p/6890084.html
Copyright © 2020-2023  润新知