Springboot连接HDFS

HDFS入门:连接与文件夹创建详解
本文介绍了如何通过HDFS URI和配置创建连接,并演示了如何在Hadoop文件系统中创建目录。关键步骤包括创建URI对象、配置连接、指定用户及使用`FileSystem.get()`方法。遇到`winutils`问题时,提供了下载和环境变量设置的解决方案。

连接HDFS并创建文件夹

1.创建URI对象,参数为HDFS对应ip端口

URI uri = new URI("hdfs://192.168.109.135:9000");

2. 创建连接配置

Configuration configuration = new Configuration();
String user="hadoop";
FileSystem fs = FileSystem.get(uri, configuration,user);

 3.代码

pom依赖导入:

<!-- hadoop 依赖 -->
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs</artifactId>
      <version>2.7.3</version>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
        </exclusion>
        <exclusion>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.7.3</version>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
        </exclusion>
        <exclusion>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>2.7.3</version>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
        </exclusion>
        <exclusion>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
      <scope>compile</scope>
    </dependency>

 方法示例:

public String hello() throws URISyntaxException, IOException, InterruptedException {

//        URI uri = new URI("hdfs://192.168.109.135:9000");
//        Configuration configuration = new Configuration();
//        String user="hadoop";
//        FileSystem fs = FileSystem.get(uri, configuration,user);
//        Path path = new Path("/dir/test");
//        fs.mkdirs(path);
//        fs.close();

        //1.获取文件系统
        Configuration configuration = new Configuration();
        String user="hadoop";
        FileSystem fs = FileSystem.get(new URI(nameNode), configuration, user);
        //2.执行操作 创建hdfs文件夹
        Path path = new Path(filePath);
        if (!fs.exists(path)) {
            fs.mkdirs(path);
        }
        //关闭资源
        fs.close();
        System.out.println("结束!");
        return "hello";
    }

可能出现报错:Could not locate executable null \bin\winutils.exe in the hadoop binaries

解决方案:winutils下载地址:winutils

                  添加到系统环境变量HADOOP_HOME重启系统

### Spring Boot 整合 HDFS 示例教程 #### 1. 添加依赖项 为了使Spring Boot能够与HDFS交互,在`pom.xml`文件中添加必要的Maven依赖项。 ```xml <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> </dependency> ``` 上述代码展示了如何引入Apache Hadoop客户端库来支持分布式文件系统的读写功能[^3]。 #### 2. 配置HDFS属性 编辑项目的`application.yml`或`.properties`文件,设置HDFS集群的具体参数: ```yaml spring: hadoop: fsUri: hdfs://namenode-host:8020/ user: root ``` 此部分定义了用于连接到特定名称节点的服务URI以及执行操作的身份验证用户名。这里提供了一个简单的例子说明怎样通过编程方式管理远程存储上的资源。 ```java @Service public class HDFSService { private final FileSystem fileSystem; @Autowired public HDFSService(Configuration configuration) throws IOException { this.fileSystem = FileSystem.get(configuration); } /** * Uploads a local file to the specified path on HDFS. */ public void uploadFile(String sourcePath, String destinationDir) throws Exception { Path srcPath = new Path(sourcePath); FileStatus[] statusList = fileSystem.globStatus(new Path(destinationDir)); if (statusList.length != 0 && !fileSystem.isDirectory(statusList[0].getPath())) { throw new IllegalArgumentException("Destination is not directory"); } fileSystem.copyFromLocalFile(false, true, srcPath, new Path(destinationDir)); } // Other methods like download, delete, etc... } ``` 这段程序片段实现了将本地计算机中的某个指定路径下的文件复制到给定的目标目录下,并进行了基本的有效性校验以防止误操作。 #### 4. 控制器接口设计 最后一步是构建RESTful API端点供外部调用者发起HTTP请求触发相应的动作。下面给出了一种可能的设计方案。 ```java @RestController @RequestMapping("/api/files") public class FileController { private final HDFSService hdfsservice; @PostMapping("/upload") public ResponseEntity<String> handleFileUpload(@RequestParam("path") String destPath, @RequestParam("file") MultipartFile multipartFile) { try { String tempFilePath = Files.createTempFile(null, null).toString(); Files.write(Paths.get(tempFilePath), multipartFile.getBytes()); hdfsservice.uploadFile(tempFilePath, destPath); return ResponseEntity.ok("Successfully uploaded!"); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); } } } ``` 该控制器接收POST类型的上传指令并将接收到的数据流保存至临时位置后再转交给之前提到过的服务实例完成实际迁移工作。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值