【问题标题】:ListFiles from HDFS Cluster来自 HDFS 集群的 ListFiles
【发布时间】:2015-02-07 14:15:36
【问题描述】:

我是 hadoop 和其他东西的业余爱好者。现在,我正在尝试访问 hadoop 集群(HDFS)并从客户端 eclipse 中检索文件列表。在hadoop java客户端上设置好所需的配置后,我可以进行以下操作。

我可以执行 copyFromLocalFilecopyToLocalFile 操作从客户端访问 HDFS。 这就是我所面临的。当我给出 listFiles() 方法时,我得到了

org.apache.hadoop.fs.LocatedFileStatus@d0085360
org.apache.hadoop.fs.LocatedFileStatus@b7aa29bf

主方法

Properties props = new Properties();
props.setProperty("fs.defaultFS", "hdfs://<IPOFCLUSTER>:8020");
props.setProperty("mapreduce.jobtracker.address", "<IPOFCLUSTER>:8032");
props.setProperty("yarn.resourcemanager.address", "<IPOFCLUSTER>:8032");
props.setProperty("mapreduce.framework.name", "yarn");
FileSystem fs = FileSystem.get(toConfiguration(props)); // Setting up the required configurations
Path p4 = new Path("/user/myusername/inputjson1/");
RemoteIterator<LocatedFileStatus> ritr = fs.listFiles(p4, true);
while(ritr.hasNext())
        {
            System.out.println(ritr.next().toString());
        }

我也尝试过 FileContext 并最终只获得了 filestatus 对象字符串或其他东西。当我迭代到远程 hdfs 目录时是否有可能获取文件名,有一个名为 getPath() 的方法,这是我们使用 hadoop API 检索文件名完整路径的唯一方法还是有任何其他方法这样我就可以只检索指定目录路径中的文件名,请帮助我解决这个问题,谢谢。

【问题讨论】:

    标签: java hadoop


    【解决方案1】:

    您确实可以使用getPath(),这将返回一个Path 对象,让您可以查询文件名。

    Path p = ritr.next().getPath();
    // returns the filename or directory name if directory
    String name = p.getName();    
    

    您获得的FileStatus 对象可以告诉您这是文件还是目录。

    这里有更多 API 文档:

    http://hadoop.apache.org/common/docs/r1.0.0/api/org/apache/hadoop/fs/Path.html

    http://hadoop.apache.org/common/docs/r1.0.0/api/org/apache/hadoop/fs/FileStatus.html

    【讨论】:

    • 其实:路径p = ritr.next().getPath();
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    相关资源
    最近更新 更多