SpringBoot集成Hadoop3.1.3


參考:https://www.cnblogs.com/ywjfx/p/11352892.html

1、pom.xml添加依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-hadoop-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<hadoop.version>3.1.3</hadoop.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!--Lombok簡化代碼-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<!-- hadoop依賴 -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

 

2、application.yml文件

server:
  port: 8877

hadoop.name-node: hdfs://localhost:9000
hadoop.namespace: /mydir

#log日志
logging:
  level:
    com:
      hadoop:
        demo:
          dao: debug

3、HdfsUtils.java

package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;

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

/**
 * hdfs基本操作
 */
@Slf4j
public class HdfsUtils {

    /**
     * 獲取文件系統
     * @param hdfsUri  nameNode地址 如"hdfs://10.10.1.142:9000"
     * @return
     */
    public static FileSystem getFileSystem(String hdfsUri) {
        //讀取配置文件
        Configuration conf = new Configuration();
        // 文件系統
        FileSystem fs = null;
        if(StringUtils.isBlank(hdfsUri)){
            // 返回默認文件系統  如果在 Hadoop集群下運行,使用此種方法可直接獲取默認文件系統
            try {
                fs = FileSystem.get(conf);
            } catch (IOException e) {
                log.error("", e);
            }
        }else{
            // 返回指定的文件系統,如果在本地測試,需要使用此種方法獲取文件系統
            try {
                URI uri = new URI(hdfsUri.trim());
                fs = FileSystem.get(uri,conf);
            } catch (Exception e) {
                log.error("", e);
            }
        }
        return fs;
    }

    /**
     * 創建文件目錄
     *
     * @param hdfsUri
     * @param path
     */
    public static void mkdir(String hdfsUri, String path) {
        try {
            // 獲取文件系統
            FileSystem fs = getFileSystem(hdfsUri);
            if(StringUtils.isNotBlank(hdfsUri)){
                path = hdfsUri + path;
            }
            // 創建目錄
            fs.mkdirs(new Path(path));
            //釋放資源
            fs.close();
        } catch (IllegalArgumentException | IOException e) {
            log.error("", e);
        }
    }

    /**
     * 刪除文件或者文件目錄
     *
     * @param path
     */
    public static void rmdir(String hdfsUri,String path) {
        try {
            // 返回FileSystem對象
            FileSystem fs = getFileSystem(hdfsUri);
            if(StringUtils.isNotBlank(hdfsUri)){
                path = hdfsUri + path;
            }
            // 刪除文件或者文件目錄  delete(Path f) 此方法已經棄用
            fs.delete(new Path(path),true);
            // 釋放資源
            fs.close();
        } catch (IllegalArgumentException | IOException e) {
            log.error("", e);
        }
    }

    /**
     * 根據filter獲取目錄下的文件
     *
     * @param path
     * @param pathFilter
     * @return String[]
     */
    public static String[] listFile(String hdfsUri, String path,PathFilter pathFilter) {
        String[] files = new String[0];
        try {
            // 返回FileSystem對象
            FileSystem fs = getFileSystem(hdfsUri);

            if(StringUtils.isNotBlank(hdfsUri)){
                path = hdfsUri + path;
            }

            FileStatus[] status;
            if(pathFilter != null){
                // 根據filter列出目錄內容
                status = fs.listStatus(new Path(path),pathFilter);
            }else{
                // 列出目錄內容
                status = fs.listStatus(new Path(path));
            }
            // 獲取目錄下的所有文件路徑
            Path[] listedPaths = FileUtil.stat2Paths(status);
            // 轉換String[]
            if (listedPaths != null && listedPaths.length > 0){
                files = new String[listedPaths.length];
                for (int i = 0; i < files.length; i++){
                    files[i] = listedPaths[i].toString();
                }
            }
            // 釋放資源
            fs.close();
        } catch (IllegalArgumentException | IOException e) {
            log.error("", e);
        }
        return files;
    }

    /**
     * 文件上傳至 HDFS
     * @param hdfsUri
     * @param delSrc       指是否刪除源文件,true為刪除,默認為false
     * @param overwrite
     * @param srcFile      源文件,上傳文件路徑
     * @param destPath     hdfs的目的路徑
     */
    public static void copyFileToHDFS(String hdfsUri,boolean delSrc, boolean overwrite,String srcFile,String destPath) {
        // 源文件路徑是Linux下的路徑,如果在 windows 下測試,需要改寫為Windows下的路徑,比如D://hadoop/djt/weibo.txt
        Path srcPath = new Path(srcFile);

        // 目的路徑
        if(StringUtils.isNotBlank(hdfsUri)){
            destPath = hdfsUri + destPath;
        }
        Path dstPath = new Path(destPath);
        // 實現文件上傳
        try {
            // 獲取FileSystem對象
            FileSystem fs = getFileSystem(hdfsUri);
            fs.copyFromLocalFile(srcPath, dstPath);
            fs.copyFromLocalFile(delSrc,overwrite,srcPath, dstPath);
            //釋放資源
            fs.close();
        } catch (IOException e) {
            log.error("", e);
        }
    }

    /**
     * 從 HDFS 下載文件
     *
     * @param srcFile
     * @param destPath 文件下載后,存放地址
     */
    public static void getFile(String hdfsUri, String srcFile,String destPath) {
        // 源文件路徑
        if(StringUtils.isNotBlank(hdfsUri)){
            srcFile = hdfsUri + srcFile;
        }
        Path srcPath = new Path(srcFile);
        Path dstPath = new Path(destPath);
        try {
            // 獲取FileSystem對象
            FileSystem fs = getFileSystem(hdfsUri);
            // 下載hdfs上的文件
            fs.copyToLocalFile(srcPath, dstPath);
            // 釋放資源
            fs.close();
        } catch (IOException e) {
            log.error("", e);
        }
    }

    /**
     * 獲取 HDFS 集群節點信息
     *
     * @return DatanodeInfo[]
     */
    public static DatanodeInfo[] getHDFSNodes(String hdfsUri) {
        // 獲取所有節點
        DatanodeInfo[] dataNodeStats = new DatanodeInfo[0];
        try {
            // 返回FileSystem對象
            FileSystem fs = getFileSystem(hdfsUri);
            // 獲取分布式文件系統
            DistributedFileSystem hdfs = (DistributedFileSystem)fs;
            dataNodeStats = hdfs.getDataNodeStats();
        } catch (IOException e) {
            log.error("", e);
        }
        return dataNodeStats;
    }

    /**
     * 查找某個文件在 HDFS集群的位置
     *
     * @param filePath
     * @return BlockLocation[]
     */
    public static BlockLocation[] getFileBlockLocations(String hdfsUri, String filePath) {
        // 文件路徑
        if(StringUtils.isNotBlank(hdfsUri)){
            filePath = hdfsUri + filePath;
        }
        Path path = new Path(filePath);

        // 文件塊位置列表
        BlockLocation[] blkLocations = new BlockLocation[0];
        try {
            // 返回FileSystem對象
            FileSystem fs = getFileSystem(hdfsUri);
            // 獲取文件目錄
            FileStatus filestatus = fs.getFileStatus(path);
            //獲取文件塊位置列表
            blkLocations = fs.getFileBlockLocations(filestatus, 0, filestatus.getLen());
        } catch (IOException e) {
            log.error("", e);
        }
        return blkLocations;
    }


    /**
     * 判斷目錄是否存在
     * @param hdfsUri
     * @param filePath
     * @param create
     * @return
     */
    public boolean existDir(String hdfsUri,String filePath, boolean create){
        boolean flag = false;

        if (StringUtils.isEmpty(filePath)){
            return flag;
        }
        try{
            Path path = new Path(filePath);
            // FileSystem對象
            FileSystem fs = getFileSystem(hdfsUri);
            if (create){
                if (!fs.exists(path)){
                    fs.mkdirs(path);
                }
            }
            if (fs.isDirectory(path)){
                flag = true;
            }
        }catch (Exception e){
            log.error("", e);
        }

        return flag;
    }
}

4、HadoopConfig.java

package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.fs.FileSystem;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URI;


@Configuration
@ConditionalOnProperty(name="hadoop.name-node")
@Slf4j
public class HadoopConfig {

    @Value("${hadoop.name-node}")
    private String nameNode;

    /**
     * Configuration conf=new Configuration();
     * 創建一個Configuration對象時,其構造方法會默認加載hadoop中的兩個配置文件,
     * 分別是hdfs-site.xml以及core-site.xml,這兩個文件中會有訪問hdfs所需的參數值,
     * 主要是fs.default.name,指定了hdfs的地址,有了這個地址客戶端就可以通過這個地址訪問hdfs了。
     * 即可理解為configuration就是hadoop中的配置信息。
     * @return
     */
    @Bean("fileSystem")
    public FileSystem createFs() throws Exception{
        //讀取配置文件
        org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();

        conf.set("fs.defalutFS", nameNode);
        conf.set("dfs.replication", "1");
        FileSystem fs = null;
        //conf.set("fs.defaultFS","hdfs://ns1");
        //指定訪問hdfs的客戶端身份
        //fs = FileSystem.get(new URI(nameNode), conf, "root");
        // 文件系統

        // 返回指定的文件系統,如果在本地測試,需要使用此種方法獲取文件系統
        try {
            URI uri = new URI(nameNode.trim());
            fs = FileSystem.get(uri,conf,"root");
        } catch (Exception e) {
            log.error("", e);
        }

        System.out.println("fs.defaultFS: "+conf.get("fs.defaultFS"));
        return  fs;
    }
}

5、HadoopTemplate.java

package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;


@Component
@ConditionalOnBean(FileSystem.class)
@Slf4j
public class HadoopTemplate {

    @Autowired
    private FileSystem fileSystem;

    @Value("${hadoop.name-node}")
    private String nameNode;

    @Value("${hadoop.namespace:/}")
    private String nameSpace;

    @PostConstruct
    public void init(){
        existDir(nameSpace,true);
    }

    public void uploadFile(String srcFile){
        copyFileToHDFS(false,true,srcFile,nameSpace);
    }

    public void uploadFile(boolean del,String srcFile){
        copyFileToHDFS(del,true,srcFile,nameSpace);
    }

    public void uploadFile(String srcFile,String destPath){
        copyFileToHDFS(false,true,srcFile,destPath);
    }

    public void uploadFile(boolean del,String srcFile,String destPath){
        copyFileToHDFS(del,true,srcFile,destPath);
    }

    public void delFile(String fileName){
        rmdir(nameSpace,fileName) ;
    }

    public void delDir(String path){
        nameSpace = nameSpace + "/" +path;
        rmdir(path,null) ;
    }

    public void download(String fileName,String savePath){
        getFile(nameSpace+"/"+fileName,savePath);
    }


    /**
     * 創建目錄
     * @param filePath
     * @param create
     * @return
     */
    public boolean existDir(String filePath, boolean create){
        boolean flag = false;
        if(StringUtils.isEmpty(filePath)){
            throw new IllegalArgumentException("filePath不能為空");
        }
        try{
            Path path = new Path(filePath);
            if (create){
                if (!fileSystem.exists(path)){
                    fileSystem.mkdirs(path);
                }
            }
            if (fileSystem.isDirectory(path)){
                flag = true;
            }
        }catch (Exception e){
            log.error("", e);
        }
        return flag;
    }




    /**
     * 文件上傳至 HDFS
     * @param delSrc       指是否刪除源文件,true為刪除,默認為false
     * @param overwrite
     * @param srcFile      源文件,上傳文件路徑
     * @param destPath     hdfs的目的路徑
     */
    public  void copyFileToHDFS(boolean delSrc, boolean overwrite,String srcFile,String destPath) {
        // 源文件路徑是Linux下的路徑,如果在 windows 下測試,需要改寫為Windows下的路徑,比如D://hadoop/djt/weibo.txt
        Path srcPath = new Path(srcFile);

        // 目的路徑
        if(StringUtils.isNotBlank(nameNode)){
            destPath = nameNode + destPath;
        }
        Path dstPath = new Path(destPath);
        // 實現文件上傳
        try {
            // 獲取FileSystem對象
            fileSystem.copyFromLocalFile(srcPath, dstPath);
            fileSystem.copyFromLocalFile(delSrc,overwrite,srcPath, dstPath);
            //釋放資源
            //    fileSystem.close();
        } catch (IOException e) {
            log.error("", e);
        }
    }


    /**
     * 刪除文件或者文件目錄
     *
     * @param path
     */
    public void rmdir(String path,String fileName) {
        try {
            // 返回FileSystem對象
            if(StringUtils.isNotBlank(nameNode)){
                path = nameNode + path;
            }
            if(StringUtils.isNotBlank(fileName)){
                path =  path + "/" +fileName;
            }
            // 刪除文件或者文件目錄  delete(Path f) 此方法已經棄用
            fileSystem.delete(new Path(path),true);
        } catch (IllegalArgumentException | IOException e) {
            log.error("", e);
        }
    }

    /**
     * 從 HDFS 下載文件
     *
     * @param hdfsFile
     * @param destPath 文件下載后,存放地址
     */
    public void getFile(String hdfsFile,String destPath) {
        // 源文件路徑
        if(StringUtils.isNotBlank(nameNode)){
            hdfsFile = nameNode + hdfsFile;
        }
        Path hdfsPath = new Path(hdfsFile);
        Path dstPath = new Path(destPath);
        try {
            // 下載hdfs上的文件
            fileSystem.copyToLocalFile(hdfsPath, dstPath);
            // 釋放資源
            // fs.close();
        } catch (IOException e) {
            log.error("", e);
        }
    }

    public String getNameSpace(){
        return nameSpace;
    }


}

6、HdfsController.java

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/hdfs")
@RestController
public class HdfsController {

    @Autowired
    private HadoopTemplate hadoopTemplate;

    /**
     * 將本地文件srcFile,上傳到hdfs
     * @param srcFile
     * @return
     */
    @RequestMapping("/upload")
    public String upload(@RequestParam String srcFile){
        hadoopTemplate.uploadFile(srcFile);
        return "upload";
    }

    @RequestMapping("/delFile")
    public String del(@RequestParam String fileName){
        hadoopTemplate.delFile(fileName);
        return "delFile";
    }

    @RequestMapping("/download")
    public String download(@RequestParam String fileName,@RequestParam String savePath){
        hadoopTemplate.download(fileName,savePath);
        return "download";
    }
}

 

遇到的問題:

1、上傳文件時報錯:

There are 0 datanode(s) running and no node(s) are excluded in this operation.

解決方案:

解決方法1:重啟linux,再使用start-dfs.sh和start-yarn.sh 重啟一下hadoop
解決辦法2:找到hadoop安裝目錄下 hadoop-2.4.1/data/dfs/data里面的current文件夾刪除
然后從新執行一下 hadoop namenode -format

2、因為修改了hadoop的環境變量,上傳時路徑不對。 重啟win10后就可以了。

 

postMan測試:

hadoop瀏覽上傳的文件:

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM