开发者社区> 问答> 正文

HDFS的API操作查看某个文件在HDFS集群中的位置怎么做?

HDFS的API操作查看某个文件在HDFS集群中的位置怎么做?

展开
收起
游客y244y7ln2rlpa 2021-12-07 06:39:42 840 0
1 条回答
写回答
取消 提交回答
  • 查看某个文件在HDFS集群中的位置:通过“FileSystem.getFileBlockLocation(FileStatus file,long start,long len)”可以查看指定文件所在位置。

    具体代码如下:``` package HDFS.learnself;

    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.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path;

    public class WhereFile { public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {

    //1.加载hdfs的配置文件 Configuration conf=new Configuration();

    //2.获取hdfs的操作对象 FileSystem fs=FileSystem.get(new URI("hdfs://hdp01:9000"), conf, "hdp02");

    //3.需要查找文件的路径 Path File=new Path("/test/fur01.txt");

    //4.创建FileStatus对象,调用getFileStatus方法 FileStatus filestatus=fs.getFileStatus(File);

    //5.创建一个BlockLocation数组对象,调用getFileBlockLocations方法 BlockLocation [] blkLocations=fs.getFileBlockLocations(filestatus, 0, filestatus.getLen()); int blockLen=blkLocations.length; for(int i=0;i<blockLen;i++){ String [] hosts=blkLocations[i].getHosts(); System.out.println("blockID:"+i+"\n"+"location:"+hosts[0]); } }

    2021-12-07 06:40:42
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Boot2.0实战Redis分布式缓存 立即下载
CUDA MATH API 立即下载
API PLAYBOOK 立即下载