Hadoop Java实现hdfs操作(2)

本文介绍如何使用Java编程语言实现对Hadoop分布式文件系统(HDFS)中文件的各种属性进行查看,包括路径、访问时间、块大小、用户组、文件大小、最后修改时间、用户名、副本数和权限等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

昨天我们用java实现了上传,重命名,创建,删除等操作。
今天我们用java实现查看文件的一些属性。

package hdfs.doit;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

public class hadoop2 {
    FileSystem hdfsClient = null;

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        //创建一个hdfs的客户端
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://192.168.72.110:9000");
        //获取root权限
        hdfsClient = FileSystem.get(new URI("hdfs://192.168.72.110:9000"), conf, "root");

    }


    @Test
    public void TextLs() throws IOException {

        //返回的是迭代器
        RemoteIterator<LocatedFileStatus> listFiles = hdfsClient.listFiles(new Path("/"), true);
//        listFiles.hasNext()返回的是boolean,没元素就退出了
        while (listFiles.hasNext()){
            //用next()方法一个一个拿取listFiles里面的元素
            LocatedFileStatus file = listFiles.next();
            System.out.println(file.getPath());//返回路径   hdfs://192.168.72.110:9000/aa/a.sh
            System.out.println(file.getAccessTime());//访问时间
            System.out.println(file.getBlockSize());//返回块的大小   我这里每块是128MB
            System.out.println(file.getGroup());//string型    用户组
            System.out.println(file.getLen());//long  文件大小
            System.out.println(file.getModificationTime());//long   最后修改时间
            System.out.println(file.getOwner());//string  用户名
            System.out.println(file.getReplication());//short  副本数
            System.out.println(file.getPermission());//LocatedFileStatus   权限
            BlockLocation[] blockLocations = file.getBlockLocations();
            for (BlockLocation block:blockLocations) {
                String[] hosts = block.getHosts();
                System.out.println("----------------");
                System.out.println(Arrays.toString(hosts));
                System.out.println(block.getLength());//文件大小 与上面的file.getLen()相同
                System.out.println(Arrays.toString(block.getNames()));
                System.out.println(block.getOffset());//long  在分块时,第二块的起始点。  (上一个块的末大小)
                System.out.println("-----------------");
            }
            
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值