2012-07-09 51 views
4

我是一个有hadoop和东西的业余爱好者。现在,我试图访问hadoop集群(HDFS)并从客户端eclipse中检索文件列表。在hadoop java客户端上设置所需的配置后,我可以执行以下操作。来自HDFS群集的ListFiles

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

[email protected] 
[email protected] 

MainMethod

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检索文件名的完整路径的唯一方法,还是有其他方法以便我可以检索指定目录路径中的文件的名称,请帮助我完成此操作,谢谢。

回答

3

确实可以使用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

+0

实际上:路径P = ritr.next()的getPath(); – 2015-02-07 01:23:41

+0

@SabaJamalian好抓,固定。 – 2015-02-07 06:15:45