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();
        }
  • 相关阅读:
    iOS开发--UILabel可以显示
    网络编程之IO模型——IO模型比较分析
    网络编程之IO模型——异步IO
    网络编程之IO模型——多路复用IO
    网络编程之IO模型——非阻塞IO
    网络编程之IO模型——阻塞IO
    Linux基本命令
    Linux界面介绍
    Linux系统目录介绍
    Linux的前世今生
  • 原文地址:https://www.cnblogs.com/lijins/p/10067749.html
Copyright © 2011-2022 走看看