HDFS目录中的文件数量 [英] File count in an HDFS directory

查看:186
本文介绍了HDFS目录中的文件数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java代码中,我想连接到HDFS中的目录,了解该目录中的文件数量,获取它们的名称并希望读取它们。我已经可以读取这些文件,但是我无法弄清楚如何计算目录中的文件并获取像普通目录一样的文件名。



为了读取我使用的DFSClient并打开文件到InputStream中。

解决方案

count

$ b

 用法:hadoop fs -count [-q]<路径> 

计算与指定文件模式匹配的路径下的目录,文件和字节数。输出列是:
DIR_COUNT,FILE_COUNT,CONTENT_SIZE FILE_NAME。

带有-q的输出列为:
QUOTA,REMAINING_QUATA,SPACE_QUOTA,REMAINING_SPACE_QUOTA,DIR_COUNT,FILE_COUNT,CONTENT_SIZE,FILE_NAME。



示例:

  hadoop fs -count hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2 
hadoop fs -count -q hdfs://nn1.example.com/file1

退出代码

0成功,错误-1。



您可以使用FileSystem并遍历路径中的文件。这里是一些示例代码

  int count = 0; 
FileSystem fs = FileSystem.get(getConf());
boolean recursive = false;
RemoteIterator< LocatedFileStatus> ri = fs.listFiles(new Path(hdfs:// my / path),递归);
while(ri.hasNext()){
count ++;
ri.next();
}


In Java code, I want to connect to a directory in HDFS, learn the number of files in that directory, get their names and want to read them. I can already read the files but I couldn't figure out how to count files in a directory and get file names like an ordinary directory.

In order to read I use DFSClient and open files into InputStream.

解决方案

count

Usage: hadoop fs -count [-q] <paths>

Count the number of directories, files and bytes under the paths that match the specified file pattern. The output columns are: DIR_COUNT, FILE_COUNT, CONTENT_SIZE FILE_NAME.

The output columns with -q are: QUOTA, REMAINING_QUATA, SPACE_QUOTA, REMAINING_SPACE_QUOTA, DIR_COUNT, FILE_COUNT, CONTENT_SIZE, FILE_NAME.

Example:

hadoop fs -count hdfs://nn1.example.com/file1 hdfs://nn2.example.com/file2
hadoop fs -count -q hdfs://nn1.example.com/file1

Exit Code:

Returns 0 on success and -1 on error.

You can just use the FileSystem and iterate over the files inside the path. Here is some example code

int count = 0;
FileSystem fs = FileSystem.get(getConf());
boolean recursive = false;
RemoteIterator<LocatedFileStatus> ri = fs.listFiles(new Path("hdfs://my/path"), recursive);
while (ri.hasNext()){
    count++;
    ri.next();
}

这篇关于HDFS目录中的文件数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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