hadoop遞迴實現列出所有檔案
阿新 • • 發佈:2019-01-08
@Test
public void listFiles() throws Exception {
// listFiles列出的是檔案資訊,而且提供遞迴遍歷
RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path(
"hdfs://hadoop0:9000/flow"), true);
while (files.hasNext()) {
LocatedFileStatus file = files.next();
Path filePath = file.getPath();
String fileName = filePath.getName();
System.out.println(fileName);
}
System.out.println("---------------------------------");
// listStatus 可以列出檔案和資料夾的資訊,但是不提供自帶的遞迴遍歷
FileStatus[] listStatus = fs.listStatus(new Path("/"));
recuersionFile(listStatus);
}
public void recuersionFile(FileStatus[] listStatus) throws Exception {
if (listStatus != null) {
for (FileStatus status : listStatus) {
if (status.isDirectory()) {
FileStatus[] listStatus2 = fs.listStatus(new Path(status.getPath().toString()));
recuersionFile(listStatus2);
}else{
String name = status.getPath().getName();
// System.out.println(status.getPath());
System.out.println("fileName: "+name);
}
}
}
}
public void listFiles() throws Exception {
// listFiles列出的是檔案資訊,而且提供遞迴遍歷
RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path(
"hdfs://hadoop0:9000/flow"), true);
while (files.hasNext()) {
LocatedFileStatus file = files.next();
Path filePath = file.getPath();
String fileName = filePath.getName();
System.out.println(fileName);
}
System.out.println("---------------------------------");
// listStatus 可以列出檔案和資料夾的資訊,但是不提供自帶的遞迴遍歷
FileStatus[] listStatus = fs.listStatus(new Path("/"));
recuersionFile(listStatus);
}
public void recuersionFile(FileStatus[] listStatus) throws Exception {
if (listStatus != null) {
for (FileStatus status : listStatus) {
if (status.isDirectory()) {
FileStatus[] listStatus2 = fs.listStatus(new Path(status.getPath().toString()));
recuersionFile(listStatus2);
}else{
String name = status.getPath().getName();
// System.out.println(status.getPath());
System.out.println("fileName: "+name);
}
}
}
}