Hadoop之API文件下载等操作

API文件下载等操作

1.下载

编写源码

@Test
    public void testLoad() throws URISyntaxException, IOException, InterruptedException {
        //1获取文件系统
        FileSystem fileSystem =
                FileSystem.get(new URI("hdfs://hadoop1002:8020"),new Configuration(),"admin");
        //2.调用api
        fileSystem.copyToLocalFile(new Path("/xiyouji/huaguoshan/sunwukong.txt"),
                new Path("d:/sunwukong2.txt"));
        //3.关闭资源
        fileSystem.close();
    }

到电脑磁盘D上查看crc是加密校验

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传在这里插入图片描述

2.HDFS文件更名和移动

 @Test
    public void testRename() throws URISyntaxException,IOException,InterruptedException {
        //1.获取文件系统
        FileSystem fileSystem =
                FileSystem.get(new URI("hdfs://hadoop1002:8020"),
                        new Configuration(),"admin");
        //2.调用api
        fileSystem.rename(new Path("/xiyouji/huaguoshan/sunwukong.txt"),
                new Path("/xiyouji/huaguoshan/meihouwang.txt"));
        //3.关闭文件系统
        fileSystem.close();
    }

查看http://hadoop1002:9870/

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传在这里插入图片描述

3. HDFS删除文件和目录

 @Test
    public void testDelete() throws Exception{
        FileSystem fileSystem =
                FileSystem.get(new URI("hdfs://hadoop1002:8020"),
                        new Configuration(),"admin");
        fileSystem.delete(new Path("/xiyouji"),true);
        fileSystem.close();
    }

查看http://hadoop1002:9870/

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传在这里插入图片描述

4.HDFS文件详情查看 查看文件名称、权限、长度、块信息

@Test
    public void testLocation() throws IOException,URISyntaxException,InterruptedException {
        FileSystem fileSystem =
                FileSystem.get(new URI("hdfs://hadoop1002:8020"),
                        new Configuration(),"admin");
        //获取所有path下的子项
        RemoteIterator<LocatedFileStatus> listFile =
                fileSystem.listFiles(new Path("/"),true);
        while(listFile.hasNext()){
            LocatedFileStatus fileStatus = listFile.next();
            System.out.println("================"+fileStatus.getPath()+"================");
            System.out.println(fileStatus.getGroup());//获取组信息
            System.out.println(fileStatus.getLen());//获取长度
            System.out.println(fileStatus.getPath().getName());//获取文件名
            System.out.println(fileStatus.getReplication());//副本数量
            System.out.println(fileStatus.getBlockSize());//块大小
            System.out.println(fileStatus.getOwner());//拥有着
            System.out.println(fileStatus.getPermission());//权限
            System.out.println(fileStatus.getModificationTime());//最后修改时间
        }
        fileSystem.close();
    }

5.HDFS文件和文件夹判断

  @Test
    public void testIsFile() throws Exception {
        FileSystem fileSystem =
                FileSystem.get(new URI("hdfs://hadoop1002:8020"),
                        new Configuration(), "admin");
        FileStatus[] listFiles =
                fileSystem.listStatus(new Path("/input"));
        for (FileStatus fileStatus : listFiles) {
            if (fileStatus.isFile()) {//判断该文件是否为文件
                System.out.println("f:" + fileStatus.getPath().getName());
            } else {
                System.out.println("d:" + fileStatus.getPath().getName());
            }
        }
        fileSystem.close();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

select_春江花月夜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值