Spring集成Hadoop实践

本文详细介绍了如何在Spring框架中集成Hadoop进行大数据处理。包括添加依赖、配置Hadoop环境、使用Spring注入FileSystem对象,以及SpringBoot环境下访问HDFS的方法。通过具体代码示例展示了创建目录、读取文件等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Spring中集成Hadoop流程梳理:

(1)maven添加spring-data-hadoop依赖

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-hadoop</artifactId>
    <version>2.5.0.RELEASE</version>
</dependency>

(2)resources中的beans.xml定义命名空间

     从官网拷贝内容粘贴:

   https://docs.spring.io/spring-hadoop/docs/2.5.0.RELEASE/reference/html/springandhadoop-config.html

<?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
        http://www.springframework.org/schema/beans ">

    <hdp:configuration id="hadoopConfiguration">
        fs.defaultFS=hdfs://hadoop000:8020
    </hdp:configuration>
    <hdp:file-system id="fileSystem" configuration-ref="hadoopConfiguration" user="root"/>
</beans>


可变的地方放进文件中application.properties,比如:spring.hadoop.fsUri=fs.defaultFS=hdfs://hadoop000:8020

<hdp:configuration id="hadoopConfiguration>  
    ${spring.hadoop.fsUri}
</hdp:configuration>

<context:property-placeholder location="application.properties"/>

(3)fileSystem通过Spring注入进来,其他方式不变

使用Spring Hadoop访问HDFS系统
Test下创建spring包,创建springHadoopApp

private ApplicationContext ctx;
private FileSystem fileSystem;

setup()
{
   ctx = new ClassPathXmlApplicationContext("beans.xml");
   fileSystem = (FileSystem)ctx.getBean("fileSystem");
}
tearDown(){
   ctx = null;
}
//创建文件夹
public void testMkdir() throws Exception{
    fileSystem.mkdirs(new Path("/springhdfs/"));
}

//读取hdfs文件内容
public void testCat() throws Exception{
    FSDataInputStream in = fileSystem.open(new Path("/springhdfs/hello.txt"));
    IOUtiles.copyBytes(in,System.out,1024);
    in.close();
}

另附:Springboot访问HDFS

(1)添加依赖

<dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-hadoop-boot</artifactId>
      <version>2.5.0.RELEASE-hadoop25</version>
 </dependency>

(2)注入FsShell

@SpringBootApplication
public class springBootHDFSApp implements CommandLineRunner

@Autowired
FsShell fsShell;//有很多方法

public void run(String... strings) throws Exception{
    for(FileStatus fileStatus:fsShell.lsr("/springhdfs")){
        //打印
    }
}

public static void main(String[] args){
    SpringApplication.run(SpringBootHDFSApp.class,args);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值