HDFS的JAVA API操作

public class HDFSApp {

    public static final String HDFS_PATH = "hdfs://192.168.228.100:9000";
    Configuration configuration;
    FileSystem fileSystem;

    @Before
    public void setUp() throws Exception {
        //configuration可以保持默认;
        // 也可以在这里一行一行写配置;
        // 也可以将hdfs-site.xml文件复制到resources文件夹(新创建)下,并"Mark Directory as" -> "Resources Root"。
        configuration = new Configuration();
        configuration.set("dfs.replication", "1");
        fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration, "hadoop");
    }

    @After
    public void tearDown() throws Exception {
        fileSystem.close();
    }

    @Test
    public void mkdir() throws Exception {
        boolean isSuccess = fileSystem.mkdirs(new Path("/ruozedata/hdfsapi"));
        //断言中写期望的值和实际的值
        Assert.assertEquals(true, isSuccess);
    }

    @Test
    public void copyFromLocalFile() throws Exception {
        Path srcPath = new Path("c:/kms10.log");
        Path dstPath = new Path("/ruozedata/hdfsapi");
        fileSystem.copyFromLocalFile(srcPath, dstPath);
    }

    @Test
    public void testReplication() {
        System.out.println(configuration.get("dfs.replication"));
    }

    @Test
    public void copyToLocalFile() throws Exception {
        Path srcPath = new Path("/ruozedata/hdfsapi/kms10.log");
        Path dstPath = new Path("D:/spark_code/test_data/kms10.log");
        fileSystem.copyToLocalFile(srcPath, dstPath);
    }

    @Test
    public void rename() throws Exception {
        Path srcPath = new Path("/ruozedata/hdfsapi/kms10.log");
        Path dstPath = new Path("/ruozedata/hdfsapi/kms10-2.log");
        fileSystem.rename(srcPath, dstPath);
    }

    @Test
    public void listFile() throws Exception {
        RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(new Path("/ruozedata"), true);
        while (files.hasNext()) {
            LocatedFileStatus fileStatus = files.next();
            String isDir = fileStatus.isDirectory() ? "文件夹" : "文件";
            String Permission = fileStatus.getPermission().toString();
            short replication = fileStatus.getReplication();
            long len = fileStatus.getLen();
            String path = fileStatus.getPath().toString();
            System.out.println(isDir + "\t" + Permission + "\t" + replication + "\t" + len + "\t" + path);

            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            for (BlockLocation location : blockLocations) {
                String[] hosts = location.getHosts();
                for (String host : hosts) {
                    System.out.println(host);
                }
            }
            //可以看出无论文件的实际大小,块大小都是128M
            long blockSize = fileStatus.getBlockSize();
            System.out.println(blockSize);
        }
    }

    @Test
    public void copyFromLocalIO() throws Exception {
        //IO 字节流 字符流
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File("D:/spark_code/test_data/kms10.log")));
        FSDataOutputStream out = fileSystem.create(new Path("/ruozedata/hdfsapi/kms10-io.log"));

        IOUtils.copyBytes(in, out, 4096, true);
//        IOUtils.closeStream(out);
//        IOUtils.closeStream(in);
    }

    @Test
    public void download01() throws Exception {

        FSDataInputStream in = fileSystem.open(new Path("/ruozedata/hdfsapi/spark-2.4.2-bin-2.6.0-cdh5.16.1.tgz"));
        FileOutputStream out = new FileOutputStream(new File("D:/spark_code/test_data/spark.tgz.part2"));

        //前几块可以用seek指定偏移量,然后buffer传输,但是最后一块不行,不然大小也是128M,最后一块只能通过IOUtils.copyBytes + seek方式
        in.seek(1024 * 1024 * 128 * 2);

//        byte[] buffer = new byte[1024];
//        for (int i = 0; i < 1024 * 128; i++) {
//            in.read(buffer);
//            out.write(buffer);
//        }

        IOUtils.copyBytes(in, out, configuration);
        IOUtils.closeStream(out);
        IOUtils.closeStream(in);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值