JAVA API操作HDFS

一. 引入POM依赖

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>3.1.3</version>
</dependency>

二. 编写API

package com.xx;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Main {

    FileSystem fileSystem;

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        // 连接集群的nn地址
        URI uri = new URI("hdfs://hadoop102:8020");
        // 创建配置
        Configuration configuration = new Configuration();
        // 获取客户端对象
        fileSystem = FileSystem.get(uri, configuration, "root");
    }

    @Test
    public void mkdirs() throws IOException {
        // 创建一个文件夹
        fileSystem.mkdirs(new Path("/hadoop-api"));
        // 关闭资源
        fileSystem.close();
    }

    @Test
    public void upload() throws IOException {
        // 文件上传(是否删除原始文件,是否覆盖目标文件,本地文件地址,hdfs文件目录)
        fileSystem.copyFromLocalFile(false, true, new Path("D:\\Study\\大数据\\hadoop\\笔记\\test.txt"), new Path("/hadoop-api"));
        // 关闭资源
        fileSystem.close();
    }

    @Test
    public void download() throws IOException {
        // 文件下载(是否删除hdfs文件,hdfs文件路径,本地路径)
        fileSystem.copyToLocalFile(false, new Path("/hadoop-api/test.txt"), new Path("D:\\Study\\大数据\\hadoop\\笔记"));
        fileSystem.close();
    }

    @Test
    public void delete() throws IOException {
        // 文件/目录删除(删除路径,是否递归删除)
        fileSystem.delete(new Path("/hadoop-api"), true);
        fileSystem.close();
    }

    @Test
    public void moveFile() throws IOException {
        // 修改文件名称/修改文件路径(原始文件路径,目标文件路径)
        fileSystem.rename(new Path("/hadoop-api/test.txt"), new Path("/hadoop-api/test-change.txt"));
        fileSystem.close();
    }

    @Test
    public void fileDetail() throws IOException {
        // 获取文件详细信息
        RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true);
        while (listFiles.hasNext()) {
            LocatedFileStatus file = listFiles.next();
            System.out.println(file.getPath());
            System.out.println(file.getPath().getName());
            System.out.println(file.getPermission());
            System.out.println(file.getOwner());
            System.out.println(file.getGroup());
            System.out.println(file.getLen());
            System.out.println(file.getModificationTime());
            System.out.println(file.getReplication());
            System.out.println(file.getBlockSize());
            System.out.println("======================");
        }
    }

    @Test
    public void judge() throws IOException {
        // 判断是文件夹还是文件
        FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses) {
            if (fileStatus.isFile()) {
                System.out.println("这是一个文件");
            } else {
                System.out.println("这是一个目录");
            }
        }
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值