• 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);
        }
    
    } 
  • 相关阅读:
    Spark面对OOM问题的解决方法及优化总结 (转载)
    spark rdd 宽窄依赖理解
    hive orc update
    hive sql 语句执行顺序及执行计划
    java 正则 贪婪匹配 匹配sql语句中的引号内容
    java 权重随机算法实现
    MySQL创建用户和加限权
    MySQL完整性约束
    MySQL基础操作与数据类型
    MySQL数据库初识
  • 原文地址:https://www.cnblogs.com/huxinga/p/6890084.html
Copyright © 2020-2023  润新知