zoukankan      html  css  js  c++  java
  • 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);
        }
    
    } 
  • 相关阅读:
    Linux命令
    Java Script Closure(js闭包)-浅谈
    Java垃圾回收机制-概述
    五、面试准备-技术扩展
    四、面试准备-前端知识
    三、面试准备-框架知识
    二、面试准备-数据库知识
    SpringBoot多环境配置+自定义属性+随机数配置
    SpringBoot入门
    Vue入门
  • 原文地址:https://www.cnblogs.com/huxinga/p/6890084.html
Copyright © 2011-2022 走看看