zoukankan      html  css  js  c++  java
  • HDFS的API操作

    1、获取FileSystem对象

        /**
         * 获取FileSystem对象
         * @return
         * @throws URISyntaxException
         * @throws IOException
         * @throws InterruptedException
         */
        private FileSystem getFs() throws URISyntaxException, IOException, InterruptedException {
            Configuration configuration = new Configuration();
            return FileSystem.get(new URI("hdfs://k8smaster:9000"),configuration,"root");
        }

    2、文件上传操作

    
    
    /**
    * 文件上传
    */
    @Test
    public void testCopyFromLocal() {
    try (FileSystem fs = getFs()) {
    //执行上传代码
    fs.copyFromLocalFile(new Path("C:\Users\12163\Desktop\1.txt"), new Path("/test"));
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
     

    参数优先级

    (1)客户端代码中设置的值 >(2)ClassPath下的用户自定义配置文件 >(3)然后是服务器的默认配置

    3、文件下载操作

        /**
         * 文件下载
         */
        @Test
        public void testCopyToLocal(){
            try (FileSystem fs = getFs()) {
                //执行下载代码
                // boolean delSrc 指是否将原文件删除
                // Path src 指要下载的文件路径
                // Path dst 指将文件下载到的路径
                // boolean useRawLocalFileSystem 是否开启文件校验
                fs.copyToLocalFile(false,new Path("/test/1.txt"), new Path("C:\Users\12163\Desktop\"),true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    4、文件删除

    /**
    * 文件删除
    */
    @Test
    public void testDelete(){
    try (FileSystem fs = getFs()){
    //当要删除的是文件夹时,设置为true,表示递归删除。删除的是文件时,设置为true/false都可以
    fs.delete(new Path("/test/1.txt"),true);
    }catch (Exception e){
    e.printStackTrace();
    }
    }

    5、文件重命名

       /**
         * 文件重命名
         */
        @Test
        public void testRename(){
            try(FileSystem fs = getFs();) {
                fs.rename(new Path("/test/1.txt"),new Path("/test/2.txt"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    6、文件详情查看

    查看文件的名称、权限、长度、块信息

    /**
         * 文件详情查看
         */
        @Test
        public void testListFiles(){
            try(FileSystem fs = getFs();) {
                RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
                while (listFiles.hasNext()) {
                    LocatedFileStatus status = listFiles.next();
                    //文件名称
                    System.err.println(status.getPath().getName());
                    //长度
                    System.err.println(status.getLen());
                    //权限
                    System.err.println(status.getPermission());
                    //分组
                    System.err.println(status.getGroup());
                    //获取存储块的信息
                    BlockLocation[] blockLocations = status.getBlockLocations();
                    for (BlockLocation blockLocation : blockLocations) {
                        //获取块存储的主节点
                        String[] hosts = blockLocation.getHosts();
                        for (String host : hosts) {
                            System.err.println(host);
                        }
                    }
                    System.err.println("============黄金分割线======================");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    7、判断是文件还是文件夹

     /**
         * 判断是文件还是文件夹
         */
        @Test
        public void testListStatus(){
            try(FileSystem fs = getFs();) {
                FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
                for (FileStatus fileStatus : fileStatuses) {
                    if (fileStatus.isFile()) {
                        System.err.println("我是文件:"+fileStatus.getPath().getName());
                    }else {
                        System.err.println("我是文件夹:"+fileStatus.getPath().getName());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
  • 相关阅读:
    Linux 查看磁盘读写速度IO使用情况
    Kubernete安装
    jenkins问题解决
    Jenkins用HTTP Request Plugin插件进行网站的监控/加探针(运维监控)
    golang信号signal的处理
    修改docker仓库资源的地址
    centos7修改hostname
    同步CentOS时间
    CentOs查那个目录占空间大
    CentOS7主机名的修改
  • 原文地址:https://www.cnblogs.com/cq-yangzhou/p/14376486.html
Copyright © 2011-2022 走看看