Hadoop之API文件下载等操作

  • Post author:
  • Post category:其他




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();
    }



版权声明:本文为a2957827124原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。