【问题标题】:How to get absolute paths of files in a directory?如何获取目录中文件的绝对路径?
【发布时间】:2013-08-03 16:11:02
【问题描述】:

我有一个包含文件、目录、子目录等的目录。如何使用 Apache Hadoop API 获取所有文件和目录的绝对路径列表?

【问题讨论】:

    标签: java hadoop bigdata


    【解决方案1】:

    使用 HDFS API:

    package org.myorg.hdfsdemo;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileStatus;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    
    
    
    public class HdfsDemo {
    
        public static void main(String[] args) throws IOException {
    
            Configuration conf = new Configuration();
            conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/core-site.xml"));
            conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/hdfs-site.xml"));
            FileSystem fs = FileSystem.get(conf);
            System.out.println("Enter the directory name :");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            Path path = new Path(br.readLine());
            displayDirectoryContents(fs, path);
        }
    
        private static void displayDirectoryContents(FileSystem fs, Path rootDir) {
            // TODO Auto-generated method stub
            try {
    
                FileStatus[] status = fs.listStatus(rootDir);
                for (FileStatus file : status) {
                    if (file.isDir()) {
                        System.out.println("This is a directory:" + file.getPath());
                        displayDirectoryContents(fs, file.getPath());
                    } else {
                        System.out.println("This is a file:" + file.getPath());
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

    • @Peter Shipilo :一个小修正,不要忘记关闭 FileSystem 实例。
    • 你需要正确的 hadoop-hdfs 依赖,等等。
    【解决方案2】:

    编写一个递归函数,它接受一个文件并检查它是否是一个目录,如果目录列出其中的所有文件并在 for 循环中检查文件是否是一个目录,然后递归调用或只返回文件列表.

    以下类似但不完全相同(这里我只返回 .java 文件)

    private static List<File> recursiveDir(File file) {
        if (!file.isDirectory()) {
    //          System.out.println("[" + file.getName() + "] is not a valid directory");
            return null;
        }
    
        List<File> returnList = new ArrayList<File>();
        File[] files = file.listFiles();
        for (File f : files) {
            if (!f.isDirectory()) {
                if (f.getName().endsWith("java")) {
                    returnList.add(f);
                }
            } else {
                returnList.addAll(recursiveDir(f));
            }
        }
        return returnList;
    }
    

    【讨论】:

    • 谢谢,但我正在搜索如何使用 Apache Hadoop 执行此操作。但现在我知道解决方案了。
    【解决方案3】:

    使用 hdfs,您可以使用 hadoop fs -lsr 。

    【讨论】:

    • hdfs dfs ls -R -- 但它没有给我们绝对路径,是吗?
    • 问题是关于 API,而不是 CLI 客户端。
    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 2012-10-04
    • 2018-12-27
    相关资源
    最近更新 更多