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();
    }

 

posted @ 2018-12-04 22:19  清湾大威少  阅读(216)  评论(0编辑  收藏  举报