Spring Boot 使用 HDFS

1 篇文章 0 订阅
1 篇文章 0 订阅

前言

hdfs是个分布式的文件系统。在 spring boot 中快速使用 hdfs 可以使用 hdfs-spring-boot-starter,而且这个是操作高可用 HDFS 的 spring boot starter,可以很方便的对 HDFS 的文件进行操作,对 HDFS 的流操作也有封装。
这个工具包使用起来比较简单,直接注入Service就能用了,比较适合进行HDFS的文件操作。而且也可以自己拓展。
对于一些hadoop的配置,也可能通过配置文件进行配置,对springboot和微服务比较友好。

一、使用

1、引入依赖

最新的版本可以去中央仓库找。

maven方式引入

<dependency>
    <groupId>bio.nvwa.boot</groupId>
    <artifactId>hdfs-spring-boot-starter</artifactId>
    <version>1.0.5</version>
</dependency>

gradle方式引入

dependencies {
    implementation 'bio.nvwa.boot:hdfs-spring-boot-starter:1.0.5'
}

2、配置yaml文件

hadoop:
  hdfs:
    # 对象池最大空闲
    maxIdle: 5
    # 对象池最大总数
    maxTotal: 20
    # 对象池最小空闲
     minIdle: 2
    # 对象池初始化连接数
    initialSize: 3
    # hdfs defaultFs
    defaultFs: hdfs://localhost:9820/ #hdfs://nameservices
    # hdfs HA设置,非HA可不写
    #nameservices: nameservices
    # hdfs HA设置,非HA可不写
    #ha: namenode1,namenode2
    # hdfs HA设置,非HA可不写
    #rpcAddress: 172.0.0.1:8020,172.0.0.2:8020

作为一个spring boot的starter,可配置化和配置简单化是一个方向。这里可以进行yaml文件配置,并且可以使用环境变量等作为配置,或者用命令参数修改配置,有这些场景的可以自己修改配置文件。同时,这个组件支持配置 HA 高可用,在使用hadoop的时候,高可用是稳定运行所必须的。

3、使用介绍

这个工具的快捷功能都在 HdfsService,注入这个service就可以使用 spring boot 操作 HDFS 了。对于一些没有的快捷方法,可以使用包内提供的HdfsService#getHdfsClient()方法进行更多的操作,不过要记得HdfsService#returnHdfsClient(HdfsClient hdfsClient),因为这里用的是对象池。
对象池的基本操作组件都已实现了,包括对象的获取和return,组件都通过aop的方式实现了自动获取了,这块操作起来还是比较方便的。

package bio.nvwa.boot.hdfs;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class TestService {

    private final static Logger LOGGER = LoggerFactory.getLogger(TestService.class);
	
    private final HdfsService hdfsService;

    public TestService(HdfsService hdfsService) {
        LOGGER.info("TestService准备构造注入");
        this.hdfsService = hdfsService;
        LOGGER.info("TestService注入HdfsService成功");
    }

    public void test() {
        LOGGER.info("hdfs测试开始");
        hdfsService.test();
        LOGGER.info("hdfs测试完成");
    }
}

二、源码分析

三、实际使用

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
集成HDFS需要使用Hadoop的API和配置文件。以下是在Spring Boot应用程序中集成HDFS的一般步骤: 1. 添加Hadoop依赖项。在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>3.3.1</version> </dependency> ``` 2. 添加Hadoop配置文件。在src/main/resources目录下创建一个hadoop文件夹,并在其中添加core-site.xml和hdfs-site.xml文件。这些文件应该包含HDFS的配置信息。 3. 创建HDFS客户端。创建一个HDFS客户端来连接和操作HDFS。以下是一个示例: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class HdfsConfiguration { @Value("${hdfs.uri}") private String hdfsUri; @Bean public FileSystem fileSystem() throws IOException { Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", hdfsUri); return FileSystem.get(configuration); } } ``` 4. 使用HDFS客户端进行文件操作。使用上面创建的HDFS客户端进行文件操作,例如上传、下载、删除等操作。以下是一个示例: ```java import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class HdfsService { @Autowired private FileSystem fileSystem; public void uploadFile(String localPath, String remotePath) throws IOException { Path localFilePath = new Path(localPath); Path remoteFilePath = new Path(remotePath); fileSystem.copyFromLocalFile(localFilePath, remoteFilePath); } public void downloadFile(String remotePath, String localPath) throws IOException { Path localFilePath = new Path(localPath); Path remoteFilePath = new Path(remotePath); fileSystem.copyToLocalFile(remoteFilePath, localFilePath); } public void deleteFile(String remotePath) throws IOException { Path remoteFilePath = new Path(remotePath); fileSystem.delete(remoteFilePath, true); } } ``` 以上是一些基本步骤,您可以根据自己的需要进行更改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值