可以使用 org.apache.hadoop.fs.FileStatus 类来输出文件在hdfs上的状态,包括路径、大小、权限、创建时间、是否为目录等许多信息,通过调用对应的 getXXX() 方法即可获取。
你可以通过 FileSystem.listStatus(Path: path) 方法,传入一个路径,来获得一个 FileStatus 数组,通过遍历数组你就能输出所有匹配到的文件的信息。
或者 你也可以用 FileSystem.getFileStatus(Path: path) 来获得单独一个目录或文件的 FileStatus。而非递归的数组。
package HomeWork;
import java.util.Scanner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileStatus;
public class quest04 {
public static void main(String[] args) {
System.out.println("请输入要查看的文件或路径:");
Scanner scanner = new Scanner(System.in);
String path = scanner.nextLine(); // hdfs的文件路径
scanner.close();
try {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
FileSystem fs = FileSystem.get(conf);
FileStatus[] status = fs.listStatus(new Path(path));
for (int i = 0; i < status.length; i++) {
System.out.println("文件路径:"+status[i].getPath());
System.out.println("读取权限:"+status[i].getPermission());
System.out.println("大小:"+status[i].getBlockSize());
System.out.println("创建时间:"+status[i].getModificationTime());
}
fs.close(); // 关闭hdfs
} catch (Exception e) {
e.printStackTrace();
}
}
}