Spring整合HDFS

来源:互联网 发布:js 表单如何拼接字符串 编辑:程序博客网 时间:2024/06/05 17:40

Spring整合HDFS

1.实例环境 

在工作中 遇到了 HDFS在项目中的使用 但是 又不想去写 这种 工具类 考虑种种 于是 让Spring去托管 首先百度了 一下有没有直接可以用的Demo结果发现并没有

于是看了看 Java的HDFS操作

分析了 发现 开发中只需要得到FileSystem实例对象即可满足需求

 

2.过程 首先需要加入Spring的包 Hadoop包 

首先我采用了 Spring的原始的Bean去注入,但是发现使用Set注入和构造注入并不能满足我的需求 之后就采用了工厂方法注入

spring.xml

 <bean id="configuratione" class="org.apache.hadoop.conf.Configuration" ></bean><bean class="org.apache.hadoop.fs.FileSystem" id="fileSystem"  factory-method="get"><constructor-arg name="uri" value="hdfs://hadoop:9000"/> <constructor-arg name="conf" ref="configuratione"/> <constructor-arg name="user" value="root"/></bean>  

这里 强调的地方:配置了 factory-method 里面调用了org.apache.hadoop.fs.FileSystem类 的方法:

这样  org.apache.hadoop.fs.FileSystem这个类就被 Spring给管理了起来 就可以实现我们的功能了

   

 后来 同事分享给我一个更方便的配置方法:

spring.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:hadoop="http://www.springframework.org/schema/hadoop"xsi:schemaLocation="http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"><!-- 配置Configuration --><hadoop:configuration></hadoop:configuration><!-- 配置FileSystem --><hadoop:file-system uri="hdfs://192.168.174.88:9000" user="root" id="fileSystem" /> <!-- 原始Bean的注入方式 <bean id="configuratione" class="org.apache.hadoop.conf.Configuration"></bean><bean class="org.apache.hadoop.fs.FileSystem" id="fileSystem"  factory-method="get"><constructor-arg name="uri" value="hdfs://hadoop:9000"/> <constructor-arg name="conf" ref="configuratione"/> <constructor-arg name="user" value="root"/></bean>   --></beans>

这样呢  使用了 hadop 标签 免去了我们麻烦的配置 以上两种就是 两种 Spring整合HDFS的过程


写了一个Demo测试类 


这样就实现了 一个上传!

1 0
原创粉丝点击