如何列出目录中的所有文件及其在hadoop hdfs中的子目录 [英] How to list all files in a directory and its subdirectories in hadoop hdfs

查看:447
本文介绍了如何列出目录中的所有文件及其在hadoop hdfs中的子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在hdfs中有一个文件夹,它有两个子文件夹,每个文件夹都有大约30个子文件夹,最后每个文件夹都包含xml文件。
我想列出只给出主文件夹路径的所有xml文件。
在本地我可以通过 apache commons-io的 FileUtils.listFiles()来实现。
我已经试过了这个

  FileStatus [] status = fs.listStatus(new Path(args [0])) ; 

但它只列出了两个第一个子文件夹,并且不会更进一步。
有没有办法在hadoop中做到这一点?

解决方案

您需要使用 FileSystem 对象并执行一些逻辑所产生的FileStatus对象可以手动递归到子目录中。



您也可以应用PathFilter来仅使用 listStatus(Path,PathFilter)方法

hadoop FsShell类有hadoop fs -lsr命令的例子,这是一个递归的LS - 参见来源,在第590行(递归步骤i s在635行触发)


I have a folder in hdfs which has two subfolders each one has about 30 subfolders which,finally,each one contains xml files. I want to list all xml files giving only the main folder's path. Locally I can do this with apache commons-io's FileUtils.listFiles(). I have tried this

FileStatus[] status = fs.listStatus( new Path( args[ 0 ] ) );

but it only lists the two first subfolders and it doesn't go further. Is there any way to do this in hadoop?

解决方案

You'll need to use the FileSystem object and perform some logic on the resultant FileStatus objects to manually recurse into the subdirectories.

You can also apply a PathFilter to only return the xml files using the listStatus(Path, PathFilter) method

The hadoop FsShell class has examples of this for the hadoop fs -lsr command, which is a recursive ls - see the source, around line 590 (the recursive step is triggered on line 635)

这篇关于如何列出目录中的所有文件及其在hadoop hdfs中的子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆