每日总结

HDFS文件详情查看

查看文件名称、权限、长度信息

@Test

public void testListFiles() throws IOException, InterruptedException, URISyntaxException {

 

// 1获取文件系统

Configuration configuration = new Configuration();

FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");

 

// 2 获取文件详情

RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);

 

while (listFiles.hasNext()) {

LocatedFileStatus fileStatus = listFiles.next();

 

System.out.println("========" + fileStatus.getPath() + "=========");

System.out.println(fileStatus.getPermission());

System.out.println(fileStatus.getOwner());

System.out.println(fileStatus.getGroup());

System.out.println(fileStatus.getLen());

System.out.println(fileStatus.getModificationTime());

System.out.println(fileStatus.getReplication());

System.out.println(fileStatus.getBlockSize());

System.out.println(fileStatus.getPath().getName());

 

// 获取块信息

BlockLocation[] blockLocations = fileStatus.getBlockLocations();

System.out.println(Arrays.toString(blockLocations));

}

// 3 关闭资源

fs.close();

}

3.2.6 HDFS文件和文件夹判断

@Test

public void testListStatus() throws IOException, InterruptedException, URISyntaxException{

 

    // 1 获取文件配置信息

    Configuration configuration = new Configuration();

    FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");

 

    // 2 判断是文件还是文件夹

    FileStatus[] listStatus = fs.listStatus(new Path("/"));

 

    for (FileStatus fileStatus : listStatus) {

 

        // 如果是文件

        if (fileStatus.isFile()) {

            System.out.println("f:"+fileStatus.getPath().getName());

        }else {

            System.out.println("d:"+fileStatus.getPath().getName());

        }

    }

 

    // 3 关闭资源

    fs.close();

}

posted @ 2021-11-25 20:26  小萌新一枚lll  阅读(72)  评论(0编辑  收藏  举报