hadoop整合spring

首先创建一个maven项目

注意几点:可能会报错的地方。

  1. 本地开发环境最好配置log4j不然的话,具体的报错信息不能定位到对的地方
  2. 既然是配置spring,我们可以直接去官网[spring config](https://docs.spring.io/spring-hadoop/docs/2.5.0.RELEASE/reference/html/springandhadoop-config.html 找maven依赖,这样一般不会出错,而且有很多帮助文档
  3. 另外我们是测试hadoop 引入junit单元测试会好点,hadoop记得提前启动
    如下所示maven依赖:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.imooc.hadoop</groupId>
    <artifactId>hadoop-train</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    
    <dependencies>
        <!--添加单元测试的依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>

        <!--添加spring hadoop依赖-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-hadoop</artifactId>
            <version>2.5.0.RELEASE</version>
        </dependency>

        <!-- 添加spring boot 依赖 -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-hadoop-boot</artifactId>
            <version>2.5.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
    <!-- 添加springg依赖-->
</project>

配置spring 的beans.xml配置文件

提供依赖注入,让开发变得高效。我们将对hadoop的请求交给spring去管理
同样这些都是可以在官方找到的配置信息 # 需要注意的是伪分布式或者分布式的话 要改一下 端口为9000
用过hadoop hdfs api的应该知道本地(windows)开发环境的话 需要配置主机-ip映射;不然后边可能也会报错
除此之外 我们最好指定一下user是谁,我这里是root否则报权限不足的错误;

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<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://hadoop:9000
                <!--hadoop.tmp.dir=/tmp/hadoop-->
                <!--electric=sea-->
        </hdp:configuration >
        <hdp:file-system id="fileSystem"
                         configuration-ref="hadoopConfiguration"
                         user="root"/>
</beans>

测试代码

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;

/**
 * spring使用
 */
public class SpringHadoopHdfsApp {
    private ApplicationContext ctx;
    private FileSystem fileSystem;  
    @Test
    public void testMkdir()throws Exception{
        fileSystem.mkdirs(new Path("/springhdfs"));//文件创建成功查看一下,手动去创建一个student.txt文本并put到hdfs该目录下
    }
    @Test
    public void testText() throws Exception{
        FSDataInputStream in = fileSystem.open(new Path("/springhdfs/student.txt")); 
        IOUtils.copyBytes(in,System.out,1024);
        in.close();
    }
    @Before
    public void setup(){
        ctx = new ClassPathXmlApplicationContext("beans.xml");
        fileSystem = (FileSystem)ctx.getBean("fileSystem");  //使用依赖注入 获取fileSystem对象
    }
    @After
    public void tearDown()throws Exception{
        ctx=null;
        fileSystem.close();
    }
}

测试结果:

在这里插入图片描述

在这里插入图片描述
更多的配置都可以通过官网去查看,将配置统统写进spring配置里边
例如hive,hbase, hdfs的输入,输出等

当然为了管理hadoop配置信息
我们还可以单独拿出来存放Hadoop的信息
创建一个application.properties
写入hadoop配置信息

# 我们可以将所有的信息以键值对的方式存入
spring.hadoop.fs-uri=hdfs://hadoop:9000

然后修改beans.xml

在这里插入图片描述
在这里插入图片描述
目录结构
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值