Hadoop 3.x|第五天|HDFS的API操作(2)

本文详细介绍了如何使用Hadoop的HDFS API进行文件系统操作,包括创建文件夹、上传和下载文件、重命名或移动文件、删除文件和目录,以及查看文件详情。通过Java代码示例展示了每个操作的实现,同时也解释了配置参数的优先级。
摘要由CSDN通过智能技术生成

创建文件夹

需要注意的是必须添加上@Test做测试才能让它运行起来。
其次是重名的类名很多,需要找到org.apache.hadoop底下的类才能成功跑起来。

public class HdfsClient {
    @Test
    public void mkdirTest() throws URISyntaxException, IOException, InterruptedException {
        //创建新配置
        Configuration con = new Configuration();
        //获取文件系统
        org.apache.hadoop.fs.FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"),con,"yjr");
        //创建文件目录
        fs.mkdirs(new Path("/mkdirtest"));
        //关闭资源
        fs.close();
    }
}

运行成功
在这里插入图片描述
此时点开网页,可以看到文件夹已经创建成功了。
在这里插入图片描述

HDFS文件上传

方法参数一:表示在本地是否删除源数据
方法参数二:表示在HDFS中是否覆盖

@Test
    public void TestCopyFromLocalFile() throws URISyntaxException, IOException, InterruptedException {
        //创建新配置
        Configuration con = new Configuration();
        //获取文件系统
        org.apache.hadoop.fs.FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"),con,"yjr");
        //上传文件
        fs.copyFromLocalFile(false,false,new Path("D:\\Desktop\\testcopy.txt"),new Path("/"));
        fs.close();
    }

上传文件参数优先级

优先级从高往低

  1. 客户端代码中设置的值,如con.set(xxx),直接在代码里指定。
  2. ClassPath 下的用户自定义配置文件,用户可在项目的resource文件夹中自己创建hdfs-site.xml之类的文件,就会按照它的走。
  3. 然后是服务器的自定义配置(xxx-site.xml),为服务器的配置。
  4. 服务器的默认配置(xxx-default.xml),为服务器的配置。

HDFS文件下载

第一个参数:是否将原文件删除
第四个参数:是否开启文件校验

    @Test
    public void TestCopyToLocalFile() throws URISyntaxException, IOException, InterruptedException {
        //创建新配置
        Configuration con = new Configuration();
        //获取文件系统
        org.apache.hadoop.fs.FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"),con,"yjr");
        //上传文件
        fs.copyToLocalFile(false, new Path("/testcopy.txt"), new Path("D:\\Desktop\\"), true);
        fs.close();
    }

文件更名或移动

    @Test
    public void TestRename() throws URISyntaxException, IOException, InterruptedException {
        //创建新配置
        Configuration con = new Configuration();
        //获取文件系统
        org.apache.hadoop.fs.FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"),con,"yjr");
        //上传文件
        fs.rename(new Path("/testcopy.txt"), new
                Path("/testrename.txt"));
        fs.close();
    }

HDFS删除文件或目录

    @Test
    public void TestDelete() throws URISyntaxException, IOException, InterruptedException {
        //创建新配置
        Configuration con = new Configuration();
        //获取文件系统
        org.apache.hadoop.fs.FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"),con,"yjr");
        //删除文件
        fs.delete(new Path("/testrename.txt"), true);
        fs.close();
    }

HDFS查看文件详情

里面多次使用了.var来创建命令语句,没看懂讲实话,查了一下var的意思大致是推断类型替换。

    @Test
    public void TestListFiles() throws URISyntaxException, IOException, InterruptedException {
        //创建新配置
        Configuration con = new Configuration();
        //获取文件系统
        org.apache.hadoop.fs.FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"),con,"yjr");
        //查看目录
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
        while (listFiles.hasNext()) {
            LocatedFileStatus fileStatus = listFiles.next();
            System.out.println("========" + fileStatus.getPath() + "=========");
            System.out.println(fileStatus.getPermission());
            System.out.println(fileStatus.getOwner());
            System.out.println(fileStatus.getGroup());
            System.out.println(fileStatus.getLen());
            System.out.println(fileStatus.getModificationTime());
            System.out.println(fileStatus.getReplication());
            System.out.println(fileStatus.getBlockSize());
            System.out.println(fileStatus.getPath().getName());
            // 获取块信息
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            System.out.println(Arrays.toString(blockLocations));
        }
        // 3 关闭资源
        fs.close();
    }

HDFS文件和文件夹判断

    @Test
    public void TestJudge() throws URISyntaxException, IOException, InterruptedException {
        //创建新配置
        Configuration con = new Configuration();
        //获取文件系统
        org.apache.hadoop.fs.FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"),con,"yjr");
        //文件状态
        FileStatus[] st = fs.listStatus(new Path("/"));

        for (FileStatus fileStatus : st) {
            // 如果是文件
            if (fileStatus.isFile()) {
                System.out.println("f:"+fileStatus.getPath().getName());
            }else {
                System.out.println("d:"+fileStatus.getPath().getName());
            }
        }
        fs.close();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值