HDFS文件詳情查看

 /**
     * HDFS文件詳情查看
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void testListFiles() throws IOException, InterruptedException, URISyntaxException{

        // 1獲取文件系統
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.2.210:9000"), configuration, "atguigu");

        // 2 獲取文件詳情
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);

        while(listFiles.hasNext()){
            LocatedFileStatus status = listFiles.next();

            // 輸出詳情
            // 文件名稱
            System.out.println(status.getPath().getName());
            // 長度
            System.out.println(status.getLen());
            // 權限
            System.out.println(status.getPermission());
            // 分組
            System.out.println(status.getGroup());

            // 獲取存儲的塊信息
            BlockLocation[] blockLocations = status.getBlockLocations();

            for (BlockLocation blockLocation : blockLocations) {

                // 獲取塊存儲的主機節點
                String[] hosts = blockLocation.getHosts();

                for (String host : hosts) {
                    System.out.println(host);
                }
            }

            System.out.println("-----------班長的分割線----------");
        }

// 3 關閉資源
        fs.close();
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章