Hadoop2.7.3客户端之Java API查看当前文件详情

Hadoop2.7.3客户端之Java API查看当前文件详情

  1. 在Idea或eclipse中创建maven项目
  2. 在pom.xml中添加hadoop依赖(参考前面博文)
  3. 编写测试代码,代码如下:
    /**
    * 查看文件的详情
    * 思路:
    * 1、配置信息Configuration
    * 2、FileSystem
    * 3、拿到文件,调用fileSystem提供的获取文件信息的方法listFiles
    * 4、关闭系统资源
    */
    public static void listFiles() throws IOException {
     //1、配置信息对象
     Configuration configuration = new Configuration();
     configuration.set("fs.defaultFS","hdfs://nn-master:9000");
     //2、拿到fileSystem对象
     FileSystem fileSystem = FileSystem.get(configuration);
     //3、调用方法查看文件的详情
     RemoteIterator<LocatedFileStatus> iterator =
     fileSystem.listFiles(new Path("/input/"), true);
     while (iterator.hasNext()){
        LocatedFileStatus locatedFileStatus = iterator.next();
        System.out.println("blockSize:" + locatedFileStatus.getBlockSize());
        System.out.println("owner:" + locatedFileStatus.getOwner());
        System.out.println("group:" + locatedFileStatus.getGroup());
        System.out.println("更新时间:" + locatedFileStatus.getModificationTime());
        System.out.println("副本数:" + locatedFileStatus.getReplication());
        BlockLocation[] blockLocations = locatedFileStatus.getBlockLocations();
        for (BlockLocation block: blockLocations) {
             System.out.println("len : " + block.getLength());
             String[] blockNames = block.getNames();
             for (String name: blockNames) {
                  System.out.println("block name : " + name);
             }
        }
     }
      //4关闭资源
      fileSystem.close();
    
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若兰幽竹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值