hdfs 遍历 / 文件夹

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;

public class listFile {
    
    
	public static void main(String [] args) throws IOException, URISyntaxException {
    
    
		FileSystem file = FileSystem.get(new URI("hdfs://Master:9000"),new Configuration());//获取FileSystem对象
		RemoteIterator<LocatedFileStatus> iterator = file.listFiles(new Path("/"), true);//调用listFiles 获取 /目录下所有的文件信息
		while(iterator.hasNext()) {
    
    //遍历 / 文件夹
			LocatedFileStatus fileStatus =  iterator.next();//获取文件状态对象
			Path path = fileStatus.getPath();//获取决定路径
			String name = path.getName();//获取名字
			System.out.println(path.toString()+"       "+path.getName());
				BlockLocation[] locations = fileStatus.getBlockLocations();//获取文件块的信息
			System.out.println("block"+blocklocations.length);//输出文件块的大小
		}
		
		
	}
}

运行结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/NewDay_/article/details/108847561
今日推荐