Hadoop集成Spring的使用詳細教程(快速入門大數(shù)據(jù))
官網(wǎng)sprng-hadoop
https://spring.io/projects/spring-hadoop
添加依賴
<dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-hadoop</artifactId> <version>2.5.0.RELEASE</version> </dependency> </dependencies>
使用spring hadoop配置及查看HDFS文件
新建資源文件beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hdp="http://www.springframework.org/schema/hadoop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd"> <hdp:configuration id="hadoopConfiguration"> fs.defaultFS=hdfs://hadoop01:9000 hadoop.tmp.dir=/tmp/hadoop electric=sea </hdp:configuration> <hdp:file-system id="fileSystem" configuration-ref="hadoopConfiguration" user="root" /> </beans>
測試文件
package com.bennyrhys.hadoop.spring; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; /** * @Author bennyrhys * @Date 1/21/21 2:35 PM */ public class SpringHadoopHDFSApp { private ApplicationContext ctx; // apache hadoop private FileSystem fileSystem; /** * 創(chuàng)建HDFS文件夾 */ @Test public void testMkdirs() throws Exception { fileSystem.mkdirs(new Path("/springhdfs")); } /** * 查看HDFS文件 */ @Test public void testText() throws Exception { FSDataInputStream in = fileSystem.open(new Path("/springhdfs/hello.txt")); IOUtils.copyBytes(in, System.out, 1024); in.close(); } @Before public void setUp() { ctx = new ClassPathXmlApplicationContext("beans.xml"); fileSystem = (FileSystem) ctx.getBean("fileSystem"); } @After public void tearDown() throws Exception { ctx = null; fileSystem.close(); } }
spring hadoop 配置文件詳解
提取變量
使用xml中的頭文件替換bean,使其允許使用上下文
${}導(dǎo)入變量
新建配置文件application.properties
spring.hadoop.fsUri=hdfs://hadoop01:9000
獲取context上下文引入變量
beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hdp="http://www.springframework.org/schema/hadoop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd"> <hdp:configuration id="hadoopConfiguration"> fs.defaultFS=${spring.hadoop.fsUri} hadoop.tmp.dir=/tmp/hadoop electric=sea </hdp:configuration> <context:property-placeholder location="application.properties"/> <hdp:file-system id="fileSystem" configuration-ref="hadoopConfiguration" user="root" /> </beans>
SpringBoot訪問HDFS系統(tǒng)
pom.xml
<!-- 添加spring boot的依賴操作hadoop --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-hadoop-boot</artifactId> <version>2.5.0.RELEASE</version> </dependency>
SpringBootHDFSApp
package com.bennyrhys.hadoop.spring; import org.apache.hadoop.fs.FileStatus; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.hadoop.fs.FsShell; /** * @Author bennyrhys * @Date 1/21/21 11:33 PM */ @SpringBootApplication public class SpringBootHDFSApp implements CommandLineRunner { @Autowired FsShell fsShell; //引入spring的 @Override public void run(String... strings) throws Exception { for (FileStatus fileStatus : fsShell.lsr("/springhdfs")) { System.out.println("> " + fileStatus.getPath()); } } /** * > hdfs://hadoop01:9000/springhdfs * > hdfs://hadoop01:9000/springhdfs/hello.txt * @param args */ public static void main(String[] args) { SpringApplication.run(SpringBootHDFSApp.class, args); } }
到此這篇關(guān)于Hadoop集成Spring的使用詳細教程(快速入門大數(shù)據(jù))的文章就介紹到這了,更多相關(guān)Hadoop集成Spring的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JeecgBoot框架升級至Spring?Boot3的實戰(zhàn)步驟
本文主要介紹了JeecgBoot框架升級至Spring?Boot3的實戰(zhàn)步驟,從?2.7.10升級到3.1.5有以下幾個點需要注意,下面就來詳細的介紹一下,感興趣的可以了解一下2024-04-04Spring使用@Async出現(xiàn)循環(huán)依賴原因及解決方案分析
在Spring框架中,啟用異步功能需要在應(yīng)用主類上添加@EnableAsync注解,當(dāng)項目中存在循環(huán)引用時,如一個異步類MessageService和一個常規(guī)類TaskService相互引用,并且這兩個類位于同一包內(nèi),這種情況下可能會觸發(fā)Spring的循環(huán)依賴異常2024-10-10Java二叉搜索樹遍歷操作詳解【前序、中序、后序、層次、廣度優(yōu)先遍歷】
這篇文章主要介紹了Java二叉搜索樹遍歷操作,結(jié)合實例形式詳細分析了Java二叉搜索樹前序、中序、后序、層次、廣度優(yōu)先遍歷等相關(guān)原理與操作技巧,需要的朋友可以參考下2020-03-03IntelliJ Idea常用11款插件(提高開發(fā)效率)
這篇文章主要介紹了IntelliJ Idea常用11款插件(提高開發(fā)效率),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Spring Boot集成mongodb數(shù)據(jù)庫過程解析
這篇文章主要介紹了Spring Boot集成mongodb數(shù)據(jù)庫過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05Java 詳解單向加密--MD5、SHA和HMAC及簡單實現(xiàn)實例
這篇文章主要介紹了Java 詳解單向加密--MD5、SHA和HMAC及簡單實現(xiàn)實例的相關(guān)資料,需要的朋友可以參考下2017-02-02