欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot集成Hadoop實(shí)現(xiàn)文件的上傳和下載功能

 更新時(shí)間:2024年07月11日 12:07:37   作者:街角等待  
Spring?Hadoop簡(jiǎn)化了Apache?Hadoop,提供了一個(gè)統(tǒng)一的配置模型以及簡(jiǎn)單易用的API來使用HDFS、MapReduce、Pig以及Hive,這篇文章主要介紹了SpringBoot集成Hadoop實(shí)現(xiàn)文件的上傳和下載,需要的朋友可以參考下

Spring Hadoop簡(jiǎn)單概述

Spring Hadoop官網(wǎng)地址如下:

https://projects.spring.io/spring-hadoop/

Spring Hadoop簡(jiǎn)化了Apache Hadoop,提供了一個(gè)統(tǒng)一的配置模型以及簡(jiǎn)單易用的API來使用HDFS、MapReduce、Pig以及Hive。還集成了其它Spring生態(tài)系統(tǒng)項(xiàng)目,如Spring Integration和Spring Batch.。

特點(diǎn):

  • 支持創(chuàng)建Hadoop應(yīng)用,配置使用依賴注入和運(yùn)行標(biāo)準(zhǔn)的java應(yīng)用程序和使用Hadoop的命令行工具。
  • 集成Spring Boot,可以簡(jiǎn)單地創(chuàng)建Spring應(yīng)用程序去連接HDFS進(jìn)行讀寫數(shù)據(jù)。
  • 創(chuàng)建和配置,使用java的MapReduce,Streaming,Hive,Pig或HBase。
  • 擴(kuò)展Spring Batch支持創(chuàng)建基于Hadoop的工作流的任何類型的Hadoop Job或HDFS的操作。
  • 腳本HDFS操作使用任何基于JVM的腳本語言。
  • 基于SpringBoot輕松地創(chuàng)建自定義的基礎(chǔ)應(yīng)用,應(yīng)用可以部署在YARN上。
  • 支持DAO,可以使用模板或回調(diào)的方式操作Hbase
  • 支持Hadoop安全驗(yàn)證

Springboot整合HBase數(shù)據(jù)庫(kù)

安裝Hadoop可參考:http://www.dbjr.com.cn/server/324127043.htm

1、pom文件中增加依賴

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>3.3.6</version>
        </dependency>
		<!--需要指定hadoop-auth、hadoop-common版本,否則容易出現(xiàn)版本沖突-->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-auth</artifactId>
            <version>3.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.3.6</version>
        </dependency>

2、在.yml配置文件中增加配置

hadoop:
  name-node: hdfs://test:8020

3、連接Hadoop庫(kù)的配置類

import lombok.extern.slf4j.Slf4j;
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;
import java.net.URI;
/**
 * @description: Hadoop配置類
 * @version: 1.0
 */
@Configuration
@Slf4j
public class HadoopConfig {
    @Value("${hadoop.name-node}")
    private String nameNode;
    @Bean("fileSystem")
    public FileSystem fileSystem() {
        //讀取配置文件
        org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
        conf.set("dfs.replication", "1");
        // 文件系統(tǒng)
        FileSystem fileSystem = null;
        // 返回指定的文件系統(tǒng),如果在本地測(cè)試,需要使用此種方法獲取文件系統(tǒng)
        try {
            URI uri = new URI(nameNode.trim());
            fileSystem = FileSystem.get(new URI(nameNode), conf, "root");
        } catch (Exception e) {
            log.error("文件系統(tǒng)獲取發(fā)生異常", e);
        }
        return fileSystem;
    }
}

4、Hadoop工具類,實(shí)現(xiàn)文件的上傳和下載

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.stereotype.Component;
import java.io.*;
/**
 * @description: hadoop操作類
 * @version: 1.0
 */
@Slf4j
@Component
public class HadoopUtil {
    @Resource
    private FileSystem fileSystem;
    /**
     * 上傳文件
     * @param file 文件
     * @param destPath 上傳的路徑
     */
    public void uploadFile(File file, String destPath) {
        try (InputStream inputStream = new FileInputStream(file);
             FSDataOutputStream outputStream = fileSystem.create(new Path(destPath))) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            // 刷新輸出流以確保所有數(shù)據(jù)都被寫入HDFS
            outputStream.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 下載文件
     * @param filePath 文件路徑
     * @return InputStream 返回inputStream類型  根據(jù)自己的需求就行轉(zhuǎn)換
     * @throws IOException
     */
    public InputStream download(String filePath) throws IOException {
        return fileSystem.open(new Path(filePath));
    }
    /**
     * 刪除文件
     * @param filePath 文件路徑
     * @throws IOException
     */
    public void deleteFile(String filePath) throws IOException {
        fileSystem.delete(new Path(filePath), true);
    }
}

5、測(cè)試

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.xxx.xxx.HadoopUtil;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@Slf4j
@Service
public class Test {
	@Resource
    private HadoopUtil hadoopUtil;
	public String test(){
		// 一、本地文件上傳hdfs
        File file = new File("/you/file/path/文件名");
        hadoopUtil.uploadFile(file, "/you/upload/path/文件名");
		// 二、下載hadoop中的文件并傳為String
        try (InputStream in = hadoopUtil.download("/文件名")) {
            String str = new Scanner(in, StandardCharsets.UTF_8).useDelimiter("\\A").next();
            log.info("結(jié)果" + str);
        } catch (IOException e) {
            log.error("文件導(dǎo)出發(fā)生異常")
        }
    }
}

6、測(cè)試hdfs中是否存在文件

命令:hdfs dfs -ls /you-file-path

[root@test app]# hdfs dfs -ls /cda
Found 1 items
-rw-r--r--   1 root supergroup      23005 2024-07-08 17:13 /cda/EMR-SD-01-病歷概要-T01-001.xml

到此這篇關(guān)于SpringBoot集成Hadoop實(shí)現(xiàn)文件的上傳和下載的文章就介紹到這了,更多相關(guān)SpringBoot文件上傳和下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot線程池監(jiān)控的簡(jiǎn)單實(shí)現(xiàn)

    springboot線程池監(jiān)控的簡(jiǎn)單實(shí)現(xiàn)

    本文主要介紹了springboot線程池監(jiān)控的簡(jiǎn)單實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Java啟用Azure Linux虛擬機(jī)診斷設(shè)置

    Java啟用Azure Linux虛擬機(jī)診斷設(shè)置

    這篇文章主要介紹了Java啟用Azure Linux虛擬機(jī)診斷設(shè)置,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • mybatis查詢語句揭秘之參數(shù)解析

    mybatis查詢語句揭秘之參數(shù)解析

    這篇文章主要給大家介紹了關(guān)于mybatis查詢語句之參數(shù)解析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • springboot整合logback實(shí)現(xiàn)日志管理操作

    springboot整合logback實(shí)現(xiàn)日志管理操作

    本章節(jié)是記錄logback在springboot項(xiàng)目中的簡(jiǎn)單使用,本文將會(huì)演示如何通過logback將日志記錄到日志文件或輸出到控制臺(tái)等管理操作,感興趣的朋友跟隨小編一起看看吧
    2024-02-02
  • 如何把idea中的項(xiàng)目導(dǎo)入github倉(cāng)庫(kù)中(圖文詳解)

    如何把idea中的項(xiàng)目導(dǎo)入github倉(cāng)庫(kù)中(圖文詳解)

    這篇文章主要介紹了如何把idea中的項(xiàng)目導(dǎo)入github倉(cāng)庫(kù)中,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • SpringCloudGateway 網(wǎng)關(guān)登錄校驗(yàn)實(shí)現(xiàn)思路

    SpringCloudGateway 網(wǎng)關(guān)登錄校驗(yàn)實(shí)現(xiàn)思路

    文章介紹了在微服務(wù)架構(gòu)中使用Spring Cloud Gateway進(jìn)行登錄校驗(yàn)的方法,通過在網(wǎng)關(guān)層面進(jìn)行登錄校驗(yàn),并將用戶信息通過請(qǐng)求頭傳遞給下游微服務(wù),解決了每個(gè)微服務(wù)都需要獨(dú)立進(jìn)行登錄校驗(yàn)的問題,此外,還討論了如何在微服務(wù)之間傳遞用戶信息
    2024-11-11
  • Mybatis中如何設(shè)置sqlSession自動(dòng)提交

    Mybatis中如何設(shè)置sqlSession自動(dòng)提交

    在MyBatis中,默認(rèn)情況下,獲取的SqlSession對(duì)象不會(huì)自動(dòng)提交事務(wù),這意味著在進(jìn)行更新、刪除或插入等操作后,需要顯式調(diào)用commit方法來提交事務(wù),但是,可以在獲取SqlSession時(shí)通過將openSession方法的參數(shù)設(shè)置為true
    2024-09-09
  • SpringBoot整合mybatis/mybatis-plus實(shí)現(xiàn)數(shù)據(jù)持久化的操作

    SpringBoot整合mybatis/mybatis-plus實(shí)現(xiàn)數(shù)據(jù)持久化的操作

    這篇文章主要介紹了SpringBoot整合mybatis/mybatis-plus實(shí)現(xiàn)數(shù)據(jù)持久化,本節(jié)內(nèi)容我們介紹了數(shù)據(jù)持久化的相關(guān)操作,并且是基礎(chǔ)傳統(tǒng)的關(guān)系型數(shù)據(jù)庫(kù)——mysql,需要的朋友可以參考下
    2022-10-10
  • Mybatis Plus 增刪改查的實(shí)現(xiàn)(小白教程)

    Mybatis Plus 增刪改查的實(shí)現(xiàn)(小白教程)

    本文主要介紹了Mybatis Plus 增刪改查,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • spring+mybatis 通過@ResponseBody返回結(jié)果中文亂碼的解決方法

    spring+mybatis 通過@ResponseBody返回結(jié)果中文亂碼的解決方法

    下面小編就為大家分享一篇spring+mybatis 通過@ResponseBody返回結(jié)果中文亂碼的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12

最新評(píng)論