使用HDFS API遍历HDFS目录树结构


/**
     * 递归遍历打印HDFS的目录树
     * @param fs FileSystem
     * @param path HDFS存储路径
     * @throws IOException
     */
    public static void iteratorShowFiles(FileSystem fs, Path path) throws IOException {
        if (fs == null || path == null) return;
        FileStatus[] listStatus = fs.listStatus(path);
        for (FileStatus fileStatus : listStatus) {
            if (fileStatus.isDirectory()) {
                System.out.println(depthString(fileStatus.getPath().depth(),">") + fileStatus.getPath().getName());
                iteratorShowFiles(fs, fileStatus.getPath());
            } else {
                System.out.println(depthString(fileStatus.getPath().depth(),"|") + fileStatus.getPath().getName());
            }
        }
    }

    /**
     * 打印层级深度的字符串数量
     * @param dept 深度
     * @param s 文件夹:>   文件: |
     * @return
     */
    public static String depthString(int dept,String s){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < dept; i++) {
            sb.append(s);
        }
        return sb.toString();
    }
    

运行效果:

>idea
||a1.txt
|merge.zip
>sanguo
>>shuguo
|||README.txt
|||cp.txt
发布了24 篇原创文章 · 获赞 27 · 访问量 6948

猜你喜欢

转载自blog.csdn.net/qq_39261894/article/details/104401710