目录

//通过工具类来操作hdfs   hdfs dfs -put d:user_info.txt  /user_info.txt 

// 将文件放入到hdfs中

@Test
    public void test1() throws IOException {  //  构建方法的时候,不要加 static 加了之后@Test就不能用了
        //操作hdfs
        //1. 初始化配置对象 需要new出来
        Configuration conf = new Configuration();
        //2. 添加配置 (其实就是提供一个key - value)
        conf.set("fs.defaultFS","hdfs://hadoop10:8020");
        //3. 构建操作hdfs的具体工具类
        FileSystem fileSystem = FileSystem.get(conf);
        //通过工具类来操作hdfs   hdfs dfs -put d:user_info.txt  /user_info.txt
        // 将文件放入到hdfs中
        fileSystem.copyFromLocalFile(new Path("d:\\user_info.txt"),new Path("/"));
        // Permission denied: 看到这个就一定要想到权限问题
        // hdfs dfs -chmod -R 777 /
        /*
          运行结果中有如下的信息,可以忽略
          SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
          SLF4J: Defaulting to no-operation (NOP) logger implementation
          SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
         */


        //关闭hdfs的连接工具
        if (fileSystem != null) {
            fileSystem.close();
        }

 2.通过工具类来操作hdfs   hdfs dfs -get hdfs路径   本地路经  将文件放入到本地Windows中

@Test
    public void test2() throws IOException {
        //操作hdfs
        //1. 初始化配置对象 需要new出来
        Configuration conf = new Configuration();
        //2. 添加配置 (其实就是提供一个key - value)
        conf.set("fs.defaultFS","hdfs://hadoop10:8020");
        //3. 构建操作hdfs的具体工具类
        FileSystem fileSystem = FileSystem.get(conf);
        //通过工具类来操作hdfs   hdfs dfs -get hdfs路径   本地路径
        // 将文件放入到本地Windows中(或者说,你的代码运行在哪里,你的本地就是哪里)
        fileSystem.copyToLocalFile(new Path("/user_info.txt"),new Path("D:\\BD2209太湖"));

        // 关闭操作工具类
        fileSystem.close();
    }

3.通过工具类来操作hdfs   hdfs dfs -mkdir -p  hdfs路径

@Test
    public void test3() throws IOException {
        //操作hdfs
        //1. 初始化配置对象 需要new出来
        Configuration conf = new Configuration();
        //2. 添加配置 (其实就是提供一个key - value)
        conf.set("fs.defaultFS","hdfs://hadoop10:8020");
        //3. 构建操作hdfs的具体工具类
        FileSystem fileSystem = FileSystem.get(conf);
        //通过工具类来操作hdfs   hdfs dfs -mkdir -p  hdfs路径
        boolean mkdir_flag = fileSystem.mkdirs(new Path("/dir_from_java/dir1/dir2"));
        System.out.println(mkdir_flag);

        // 关闭操作工具类
        fileSystem.close();
    }

4.通过工具类来操作hdfs  查看一个文件是否存在

@Test
    public void test4() throws IOException {
        //操作hdfs
        //1. 初始化配置对象 需要new出来
        Configuration conf = new Configuration();
        //2. 添加配置 (其实就是提供一个key - value)
        conf.set("fs.defaultFS","hdfs://hadoop10:8020");
        //3. 构建操作hdfs的具体工具类
        FileSystem fileSystem = FileSystem.get(conf);
        //通过工具类来操作hdfs  查看一个文件是否存在
        boolean exists = fileSystem.exists(new Path("/suibian"));
        System.out.println(exists);

        boolean exists1 = fileSystem.exists(new Path("/test1"));
        System.out.println(exists1);

        // 关闭操作工具类
        fileSystem.close();
    }

5.删除一个hdfs中的文件

@Test
    public void test5() throws IOException {
        //操作hdfs
        //1. 初始化配置对象 需要new出来
        Configuration conf = new Configuration();
        //2. 添加配置 (其实就是提供一个key - value)
        conf.set("fs.defaultFS","hdfs://hadoop10:8020");
        //3. 构建操作hdfs的具体工具类
        FileSystem fileSystem = FileSystem.get(conf);
        //通过工具类来操作hdfs  hdfs dfs -rm hdfs绝对路径
        // 删除一个hdfs中的文件
        //  delete 方法自带 -r 参数,也就是直接调用的时候,就会递归删除
        // 如果想要避免直接递归删除,可以使用第二个参数, false 来进行控制
        boolean delete = fileSystem.delete(new Path("/test"),false);
        System.out.println(delete);

        // 关闭操作工具类
        fileSystem.close();
    }

6.查看某一个路径下的所有文件以及文件夹

@Test
    public void test6() throws IOException {
        //操作hdfs
        //1. 初始化配置对象 需要new出来
        Configuration conf = new Configuration();
        //2. 添加配置 (其实就是提供一个key - value)
        conf.set("fs.defaultFS","hdfs://hadoop10:8020");
        //3. 构建操作hdfs的具体工具类
        FileSystem fileSystem = FileSystem.get(conf);
        //通过工具类来操作hdfs  hdfs dfs -ls hdfs绝对路径
        // 查看某一个路径下的所有文件以及文件夹
        // listStatus 返回值是一个对象数组,那么为什么要返回一个对象数组呢?直接给一个String 数组不就行了吗?
        // 对象中可以拥有属性和方法,一个对象里面可以存储大量的信息
        FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses) {
            System.out.println(fileStatus.getPath());
            System.out.println("是不是一个文件夹:"+fileStatus.isDirectory());
            System.out.println("文件大小是:"+fileStatus.getLen());
            System.out.println("副本数:"+fileStatus.getReplication());
            System.out.println("------------------");
        }

        // 关闭操作工具类
        fileSystem.close();
    }

7.通过工具类来操作hdfs  查看关于数据块的信息

@Test
    public void test7() throws IOException {
        //操作hdfs
        //1. 初始化配置对象 需要new出来
        Configuration conf = new Configuration();
        //2. 添加配置 (其实就是提供一个key - value)
        conf.set("fs.defaultFS","hdfs://hadoop10:8020");
        //3. 构建操作hdfs的具体工具类
        FileSystem fileSystem = FileSystem.get(conf);
        //通过工具类来操作hdfs  查看关于数据块的信息
        // 文件本身对于 hdfs来说,是一个逻辑上的概念
        // listFiles 方法可以返回一个存放对象的迭代器,这个对象中有 数据块的信息
        // 查看一个迭代器中的内容时,使用while循环,while循环的刹车踏板:hasNext()
        // hasNext()返回的值是false的时候,就算是读完了迭代器,返回true的时候,就是还有下一个元素需要读取
        RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(new Path("/"), true);
        while (iterator.hasNext()){
            LocatedFileStatus fileStatus = iterator.next();
            System.out.println(fileStatus.getLen());
            System.out.println(fileStatus.getPath());
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            //  这个循环会循环几次,是根据一个文件被拆成了多少个块决定的
            for (BlockLocation blockLocation : blockLocations) {
                String[] hosts = blockLocation.getHosts();
                String strHosts = Arrays.toString(hosts);
                String names = Arrays.toString(blockLocation.getNames());
                long offset = blockLocation.getOffset();
                long length = blockLocation.getLength();
                System.out.println("所在的DataNode是:"+names+" 所在主机:"+strHosts+" 偏移量是:"+offset+" 块的大小是:"+length);
            }
            System.out.println("--------------");
        }
        // 关闭操作工具类
        fileSystem.close();
    }

8.查看所有文件

 @Test
    public void test3() throws IOException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://hadoop10:8020");
        FileSystem fileSystem = FileSystem.get(conf);
            //2:调用方法listFiles 获取 /目录下所有的文件信息,,参数true代表递归遍历
            RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(new Path("/"), true);
            //3:遍历迭代器
            while (iterator.hasNext()){
                LocatedFileStatus fileStatus = iterator.next();
                //getPath()方法就是获取绝对路径
                System.out.println(fileStatus.getPath() + "----" +fileStatus.getPath().getName());

                //文件的block信息
                BlockLocation[] blockLocations = fileStatus.getBlockLocations();
                for (BlockLocation blockLocation : blockLocations) {
                    String[] hosts = blockLocation.getHosts();
                    String strHosts = Arrays.toString(hosts);
                    String names = Arrays.toString(blockLocation.getNames());
                    long offset = blockLocation.getOffset();
                    long length = blockLocation.getLength();
                    System.out.println("所在的DataNode是:"+names+" 所在主机:"+strHosts+" 块的大小是:"+length);
                }

            }
        // 关闭操作工具类
        fileSystem.close();

    }

更多推荐