我是 hadoop 和其他东西的业余爱好者。现在,我正在尝试访问 hadoop 集群 (HDFS) 并从客户端 eclipse 检索文件列表。我在hadoop java client上设置好需要的配置后就可以进行如下操作了。
我可以执行copyFromLocalFile、copyToLocalFile 操作从客户端访问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 检索文件名完整路径的唯一方法还是有任何其他方法这样我就只能检索指定目录路径中文件的名称,请帮助我解决这个问题,谢谢。
最佳答案
您确实可以使用 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
关于java - HDFS 集群中的 ListFiles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11393966/