hdfs client api

public class HdfsClient {
    FileSystem fs = null;

    @Before
    public void init() throws Exception {
        Configuration conf = new Configuration();
//        conf.set("fs.defaultFS","hdfs://mini01:9000");
        fs = FileSystem.get(new URI("hdfs://mini01:9000"), conf, "root");
    }

    @Test
    public void testUpload() throws IOException {
        fs.copyFromLocalFile(new Path("/home/lzq/250.txt"), new Path("/"));
        fs.close();
    }

    @Test
    public void testDownload() throws IOException {
        fs.copyToLocalFile(new Path("/250.txt"), new Path("/home/lzq/Test"));
        fs.close();
    }

    @Test
    public void testMkdir() throws IOException {
        boolean mkdir = fs.mkdirs(new Path("/home/lzq"));
        System.out.println(mkdir);
    }

    @Test
    public void deletetest() throws IOException {
        boolean b = fs.delete(new Path("/home/lzq"), true);
        System.out.println(b);
    }

    public void testLs() throws IOException {
        //返回是迭代器,而不是list因为list等占内存,那么多数据内存一下就爆掉了,所以返回是
        //iterator,每次next返回一个值,类是与py yield generator
        RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fs.listFiles(new Path("/"), true);
        while (locatedFileStatusRemoteIterator.hasNext()) {
            LocatedFileStatus file = locatedFileStatusRemoteIterator.next();
            System.out.println(file.getAccessTime());
            System.out.println(file.getOwner());
            System.out.println(file.getReplication());
            System.out.println(file.getBlockLocations());
            BlockLocation[] blockLocations = file.getBlockLocations();
//            一个文件可能会有多个块,所以是数组
            for (BlockLocation bl : blockLocations) {
                System.out.println(bl.getOffset());
                System.out.println(bl.getLength());
//                一个快在多个主机上
                String[] hosts = bl.getHosts();
                for (String host : hosts) {
                    System.out.println(host);
                }
            }
        }
    }

    public void testl() throws IOException {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
//        返回目录

        for (FileStatus file : fileStatuses) {
            System.out.println();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值