zoukankan      html  css  js  c++  java
  • hdfs 操作 入门api

    获取分布式文件系统

        // 获取文件系统
        @Test
        public void getFileSystem() throws Exception{
            
            Configuration configuration = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"), 
                    configuration, "ljs");
            System.out.println(fs);
            fs.close();
        }

    URI 对象是指向hadoop集群中的namenode 节点, 端口也是配置的  , user = “”ljs“”  用户

    上传文件到hdfs文件系统

        //上传文件系统
        //@Test
        public void putFileTohdfs() throws Exception{
            
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"),
                    conf, "ljs");
            fs.copyFromLocalFile(true, new Path("d:/text"), new Path("/user/ljs/"));
            fs.close();
            
        }
        

    从hdfs文件系统下载文件

    public void getFileHDFS() throws Exception{
            
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"), conf, "ljs");
            
            fs.copyToLocalFile(false,new Path("/user/ljs/w.txt"), new Path("d:/Demo/w.txt"),true);
        }

    给hdfs文件系统创建目录s

    @Test
        public void mkdirAtHDFs() throws Exception{
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"), conf, "ljs");
            fs.mkdirs(new Path("user/ljs/output"));
            fs.close();
        }

    删除某文件

        @Test
        public void deleteAtHDFS() throws Exception{
            //获取文件系统
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"), conf, "ljs");
    
            //执行删除
            fs.delete(new Path("/user/ljs/text"),true);
            
            fs.close();
            
        }

    给某个文件或目录改名字

    @Test
        public void renameAtHDFS() throws Exception{
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.59.11:9000"), conf, "ljs");
            fs.rename(new Path("/user/ljs/user"), new Path("/user/ljs/SB"));
            
            fs.close();
        }
  • 相关阅读:
    go 注释/说明/文档 标注
    go stack object escape
    ubuntu virtualBox windows10 CPU占用100%
    git 团队合作
    git 修改远程pull和push地址
    go 项目编译失败
    fork函数 linux创建子进程
    51nod1183 编辑距离
    各种平衡树
    redis 配置多个ip 解决方案
  • 原文地址:https://www.cnblogs.com/lijins/p/10067749.html
Copyright © 2011-2022 走看看