當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
使用Spring-hadoop小结
生活随笔
收集整理的這篇文章主要介紹了
使用Spring-hadoop小结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SpringHadoop是通過Spring框架來調用hdfs,跟直接調用hdfs的最大的不同區別是Spring通過依賴注入的方式生成操作hdfs所需要的configuration和filesystem對象,其他所有調用hdfs的api不變
1.在項目的main目錄下創建resources文件夾,并將其添加到項目的資源文件中,如圖

2.在resources文件夾下創建beans.xml和application.properties文件,分別用來注入對象及管理配置文件

3.添加hadoop和spring-hadoop的依賴
<!--在這里可以定義變量,可以統一管理版本號--> <properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--對hadoop版本進行統一管理--><hadoop.version>2.6.0-cdh5.7.0</hadoop.version> </properties><repositories><!--添加下載hadoop的倉庫--><repository><id>cloudera</id><url>https://repository.cloudera.com/artifactory/cloudera-repos</url></repository> </repositories><dependencies><!--添加hadoop依賴--><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>${hadoop.version}</version><!--由于生產環境上已經有這個包,因此不需要打到項目里去--><scope>provided</scope></dependency><!--添加spring-hadoop依賴--><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-hadoop</artifactId><version>2.5.0.RELEASE</version></dependency></dependencies>4.配置beans.xml和application.properties文件中的configuration和filesystem對象
beans.xml文件
<!--將屬性配置文件application.properties引用進來,這樣在該xml文件就可以使用該配置文件里面的屬性--> <context:property-placeholder location="application.properties"/><!--配置hdfs的configurtion--> <hdp:configuration id="hadoopConfiguration" ><!--配置namenode的地址-->fs.defaultFS=${spring.hadoop.fs-uri} </hdp:configuration><hdp:file-system id="fileSystem" configuration-ref="hadoopConfiguration" user="root"/>application.properties文件
spring.hadoop.fs-uri=hdfs://hadoop01:80205.最后是java單元測試代碼
package 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 ht* @create 2018-01-30 23:03* @desc SpringHadoop測試類**/ public class SpringHadoopApp {//Spring上下文private ApplicationContext mContext;//hdfs文件系統對象private FileSystem mFileSystem;/*** 測試創建文件夾*/@Testpublic void mkdir() throws IOException {mFileSystem.mkdirs(new Path("/test"));}/*** 測試從服務器讀取文件*/@Testpublic void text() throws IOException {FSDataInputStream is = mFileSystem.open(new Path("/test/install.log.syslog"));IOUtils.copyBytes(is,System.out,1024);is.close();}@Beforepublic void setUp() {//獲取Spring上下文,spring的依賴注入,是將對象注入到beans中,類似dagger2中的moudle,專門負責生成對象mContext = new ClassPathXmlApplicationContext("beans.xml");//通過beans.xml文件獲取filesystem對象mFileSystem = (FileSystem) mContext.getBean("fileSystem");}@Afterpublic void tearDown() throws IOException {mContext = null;mFileSystem.close();} }轉載于:https://www.cnblogs.com/flowyourheart/p/shi-yongSpringhadoop-xiao-jie.html
總結
以上是生活随笔為你收集整理的使用Spring-hadoop小结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: import
- 下一篇: 洛谷P4559 [JSOI2018]列队