我是hadoop和东西的业余爱好者.现在,我正在尝试访问hadoop集群(HDFS)并从客户端eclipse中检索文件列表.在hadoop java客户端上设置所需的配置后,我可以执行以下操作.
我可以执行copyFromLocalFile,copyToLocalFile操作从客户端访问HDFS.这就是我所面对的.当我给出listFiles()方法时,我得到了
org.apache.hadoop.fs.LocatedFileStatus@d0085360
org.apache.hadoop.fs.LocatedFileStatus@b7aa29bf
Run Code Online (Sandbox Code Playgroud)
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());
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过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();
Run Code Online (Sandbox Code Playgroud)
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