46-54-hadoop-HDFS-api

51 篇文章 1 订阅
27 篇文章 0 订阅

46-hadoop-hdfs-api:

HDFS-API-window环境准备

1、准备hadoop依赖

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KhvROEyQ-1668929813870)(png/1619357273118.png)]

2、配置环境变量

在这里插入图片描述

双击winutils.exe(一闪而过)

创建maven项目,略过

添加依赖

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.1.3</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.30</version>
        </dependency>
    </dependencies>

在项目的 src/main/resources 目录下,新建一个文件,命名为“log4j.properties”,在文件中填入

log4j.rootLogger=INFO, stdout 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n 
log4j.appender.logfile=org.apache.log4j.FileAppender 
log4j.appender.logfile.File=target/spring.log 
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout 
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
创建目录测试
    private FileSystem fileSystem;
    
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        //连接集群namenode地址
        URI uri = new URI("hdfs://192.168.1.102:8020");
    
        //配置文件
        Configuration configuration = new Configuration();
    
        //获取客户端对象
         fileSystem = FileSystem.get(uri, configuration,"root");
    }
    @After
    public void close() throws IOException {
        //关闭资源
        fileSystem.close();
    }
    @Test
    public void testMkdir() throws URISyntaxException, IOException, InterruptedException {
        //执行相关操作,创建一个文件夹
        fileSystem.mkdirs(new Path("/xiyou/ huaguosan1"));
    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-m8Sifg7S-1668929813872)(png/1619359187841.png)]

上传

/**参数优先级 hdfs-default.xml=>dfs-site.xml=>resource目录下的hdfs-site.xml =>//配置文件 Configuration configuration = new Configuration(); * configuration.set(“dfs.replication”,“2”); */

    //上传api测试
    @Test
    public void testPut() throws IOException {
        //参数,1是否删除元数据,2如果目标已经存在,是否覆盖 3.元数据路径 4.目标路径
        fileSystem.copyFromLocalFile(false,false,
                new Path("D:\\Study\\test-txt\\quninainaidetui.txt"),
                new Path("hdfs://hadoop102/xiyou/huaguosan"));
    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aExtqXoE-1668929813872)(png/1619360805608.png)]

下载

.crc是一种校验数据的方式(加密,校验。crc加密算法。对比)

    //文件的下载
    @Test
    public void testGet() throws IOException {
        //参数1:源文件是否删除   2:源文件路径 3:win目标路径 4:
        fileSystem.copyToLocalFile(false,new Path("hdfs://hadoop102/xiyou/huaguosan"),
                new Path("D:\\test"),false);
    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OCwcsw5b-1668929813873)(png/1619361739396.png)]

文件删除
    //文件删除
    @Test
    public void testRm() throws IOException {
        //1,删除路径    2:是否递归删除
        //删除文件
        fileSystem.delete(new Path("hdfs://hadoop102/xiyou/huaguosan/quninainaidetui.txt"),false);

        //删除空目录
        fileSystem.delete(new Path("hdfs://hadoop102/xiyou/huaguosan/"),false);

        //删除非空目录
        fileSystem.delete(new Path("hdfs://hadoop102/jinguo"),true);
    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9bVr4EhF-1668929813875)(png/1619362335546.png)]

文件名称的修改和移动

    //文件的更名和移动
    @Test
    public void testMv() throws IOException {
        //1:源文件路径 2:目标文件路径
        //文件名称的修改
        //fileSystem.rename(new Path("hdfs://hadoop102/jinguo/put-shell-hdfs.txt"),
                new Path("hdfs://hadoop102/jinguo/xiugaihou-put-shell-hdfs.txt"));
        
        
        //文件的移动及更名
        fileSystem.rename(new Path("hdfs://hadoop102/jinguo/xiugaihou-put-shell-hdfs.txt"),
                new Path("/移动及更名-put-shell-hdfs.txt"));
                
        //目录的更名
        fileSystem.rename(new Path("hdfs://hadoop102/jinguo"),
                new Path("/jinguo02"));
    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PfOFqpmU-1668929813875)(png/1619449461557.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lcHFvQBy-1668929813875)(png/1619449485692.png)]

查看文件名称、权限、长度、块信息

    //获取文件详细信息
    @Test
    public void fileDetail() throws IOException {
        //1:路径 2:是否迭代
        //获取所有文件信息
        RemoteIterator<LocatedFileStatus> listFiles = fileSystem.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));
        }

        // 关闭资源
        fileSystem.close();
    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-csgR3xAJ-1668929813876)(png/1619450163073.png)]

HDFS 文件和文件夹判断

    @Test
    public void testListStatus() throws IOException, InterruptedException,
            URISyntaxException{
        // 1 获取文件配置信息
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"),
                configuration, "root");
        // 2 判断是文件还是文件夹
        FileStatus[] listStatus = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : listStatus) {
            // 如果是文件
            if (fileStatus.isFile()) {
                System.out.println("是文件:"+fileStatus.getPath().getName());
            }else {
                System.out.println("是文件夹:"+fileStatus.getPath().getName());
            }
        }
        // 3 关闭资源
        fs.close();
    }

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O4Z8RRcZ-1668929813876)(png/1619450448979.png)]

学习路径:https://space.bilibili.com/302417610/,如有侵权,请联系q进行删除:3623472230

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值