Hadoop基础-HDFS递归列出文件系统-FileStatus与listFiles两种方法

时间:2021-12-08 09:32:44

            Hadoop基础-HDFS递归列出文件系统-FileStatus与listFiles两种方法

                                          作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  fs.listFiles方法,返回LocatedFileStatus的迭代器,自带递归。但是它是继承于FileStatus的,而且构建函数是FileStatus的文件版,即LocaledFileStatus只能列出文件。接下来我我们一起看看这两个方法的用法。

一.listStatus方法

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.day01.note1; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils; import java.io.FileOutputStream;
import java.io.IOException; public class HdfsDemo2 {
public static void main(String[] args) throws IOException {
list();
System.out.println("====== 我是分割线 ========");
tree("/shell");
} //查看指定路径的树形结构,类似于Linux的tree命令。
private static void tree(String srcPath) throws IOException {
//由于我的Hadoop完全分布式根目录对yinzhengjie以外的用户(尽管是root用户也没有写入权限哟!因为是hdfs系统,并非Linux系统!)没有写入
// 权限,所以需要手动指定当前用户权限。使用“HADOOP_USER_NAME”属性就可以轻松搞定!
System.setProperty("HADOOP_USER_NAME","yinzhengjie");
//实例化一个Configuration,它会自动去加载本地的core-site.xml配置文件的fs.defaultFS属性。(该文件放在项目的resources目录即可。)
Configuration conf = new Configuration();
//代码的入口点,初始化HDFS文件系统,此时我们需要把读取到的fs.defaultFS属性传给fs对象。
FileSystem fs = FileSystem.get(conf);
//这个path是指是需要在文件系统中写入的数据,里面的字符串可以写出“hdfs://s101:8020/shell”,但由于core-site.xml配置
// 文件中已经有“hdfs://s101:8020”字样的前缀,因此我们这里可以直接写相对路径即可
Path path = new Path(srcPath);
//通过fs的listStatus方法获取一个指定path的所有文件信息(status),因此我们需要传入一个hdfs的路径,返回的是一个filStatus数组
FileStatus[] fileStatuses = fs.listStatus(path);
for (FileStatus fileStatus : fileStatuses) {
//判断当前迭代对象是否是目录
if (fileStatus.isDirectory()){
String dirPath = fileStatus.getPath().toString();
System.out.println("文件夹名:" + fileStatus.getPath());
tree(dirPath);
}else {
System.out.println("文件名:" + fileStatus.getPath());
}
} } //查看指定路径下的所有文件
private static void list() throws IOException {
//由于我的Hadoop完全分布式根目录对yinzhengjie以外的用户(尽管是root用户也没有写入权限哟!因为是hdfs系统,并非Linux系统!)没有写入
// 权限,所以需要手动指定当前用户权限。使用“HADOOP_USER_NAME”属性就可以轻松搞定!
System.setProperty("HADOOP_USER_NAME","yinzhengjie");
//实例化一个Configuration,它会自动去加载本地的core-site.xml配置文件的fs.defaultFS属性。(该文件放在项目的resources目录即可。)
Configuration conf = new Configuration();
//代码的入口点,初始化HDFS文件系统,此时我们需要把读取到的fs.defaultFS属性传给fs对象。
FileSystem fs = FileSystem.get(conf);
//这个path是指是需要在文件系统中写入的数据,里面的字符串可以写出“hdfs://s101:8020/shell”,但由于core-site.xml配置
// 文件中已经有“hdfs://s101:8020”字样的前缀,因此我们这里可以直接写相对路径即可
Path path = new Path("/shell");
//通过fs的listStatus方法获取一个指定path的所有文件信息(status),因此我们需要传入一个hdfs的路径,返回的是一个filStatus数组
FileStatus[] fileStatuses = fs.listStatus(path);
for (FileStatus fileStatus : fileStatuses) {
//判断当前迭代对象是否是目录
boolean isDir = fileStatus.isDirectory();
//获取当前文件的绝对路径
String fullPath = fileStatus.getPath().toString();
System.out.println("isDir:" + isDir + ",Path:" + fullPath);
}
}
} /*
以上代码执行结果如下:
isDir:true,Path:hdfs://s101:8020/shell/awk
isDir:true,Path:hdfs://s101:8020/shell/grep
isDir:true,Path:hdfs://s101:8020/shell/sed
isDir:false,Path:hdfs://s101:8020/shell/yinzhengjie.sh
====== 我是分割线 ========
文件夹名:hdfs://s101:8020/shell/awk
文件名:hdfs://s101:8020/shell/awk/keepalive.sh
文件名:hdfs://s101:8020/shell/awk/nginx.conf
文件夹名:hdfs://s101:8020/shell/grep
文件名:hdfs://s101:8020/shell/grep/1.txt
文件名:hdfs://s101:8020/shell/grep/2.txt
文件夹名:hdfs://s101:8020/shell/sed
文件名:hdfs://s101:8020/shell/sed/nagios.sh
文件名:hdfs://s101:8020/shell/sed/zabbix.sql
文件名:hdfs://s101:8020/shell/yinzhengjie.sh
*/

二.listFiles方法

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.day01.note1; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils; import java.io.FileOutputStream;
import java.io.IOException; public class HdfsDemo3 {
public static void main(String[] args) throws IOException {
autoList("/shell");
} //定义方法下载文件到本地
private static void autoList(String path) throws IOException {
//实例化一个Configuration,它会自动去加载本地的core-site.xml配置文件的fs.defaultFS属性。(该文件放在项目的resources目录即可。)
Configuration conf = new Configuration();
//代码的入口点,初始化HDFS文件系统,此时我们需要把读取到的fs.defaultFS属性传给fs对象。
FileSystem fs = FileSystem.get(conf);
//通过fs的listFiles方法可以自动实现递归(自带递归)列出文件类型,返回的是一个远程可迭代对象,需要传入两个参数,第一个参数是服务器路径,第二个参数是否递归
RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path(path), true);
while (iterator.hasNext()){
LocatedFileStatus fileStatus = iterator.next();
Path fullPath = fileStatus.getPath();
System.out.println(fullPath);
}
}
} /*
以上代码执行结果如下:
hdfs://s101:8020/shell/awk/keepalive.sh
hdfs://s101:8020/shell/awk/nginx.conf
hdfs://s101:8020/shell/grep/1.txt
hdfs://s101:8020/shell/grep/2.txt
hdfs://s101:8020/shell/sed/nagios.sh
hdfs://s101:8020/shell/sed/zabbix.sql
hdfs://s101:8020/shell/yinzhengjie.sh
*/