Hadoop集成Spring的使用详细教程(快速入门大数据)

 更新时间:2021年01月22日 09:46:12   作者:瑞 新  
这篇文章主要介绍了Hadoop集成Spring的使用详细教程(快速入门大数据),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

官网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;

 /**
  * 创建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,使其允许使用上下文
${}导入变量

新建配置文件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系统

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);
 }
}

到此这篇关于Hadoop集成Spring的使用详细教程(快速入门大数据)的文章就介绍到这了,更多相关Hadoop集成Spring的使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java解析php函数json_encode unicode 编码问题

    java解析php函数json_encode unicode 编码问题

    这篇文章主要介绍了java解析php函数json_encode unicode 编码问题,需要的朋友可以参考下
    2016-04-04
  • Java8 用Lambda表达式给List集合排序的实现

    Java8 用Lambda表达式给List集合排序的实现

    这篇文章主要介绍了Java8 用Lambda表达式给List集合排序的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • 基于eclipse-temurin镜像部署spring boot应用的实现示例

    基于eclipse-temurin镜像部署spring boot应用的实现示例

    本文提供了基于eclipse-temurin镜像部署Spring Boot应用的详细实现示例,通过使用Docker镜像,可以轻松地创建和管理Spring Boot应用程序的容器化环境,感兴趣的可以了解一下
    2023-08-08
  • Java实现二叉树的建立、计算高度与递归输出操作示例

    Java实现二叉树的建立、计算高度与递归输出操作示例

    这篇文章主要介绍了Java实现二叉树的建立、计算高度与递归输出操作,结合实例形式分析了Java二叉树的创建、遍历、计算等相关算法实现技巧,需要的朋友可以参考下
    2019-03-03
  • java实现插入排序算法

    java实现插入排序算法

    插入排序算法是一个对少量元素进行排序的有效算法。插入排序的工作原理与打牌时整理手中的牌的做法类似,开始摸牌时,我们的左手是空的,接着一次从桌上摸起一张牌,并将它插入到左手的正确位置。
    2015-04-04
  • Spring中的@ExceptionHandler注解统一异常处理详解

    Spring中的@ExceptionHandler注解统一异常处理详解

    这篇文章主要介绍了Spring中的@ExceptionHandler注解统一异常处理详解,当我们使用这个@ExceptionHandler注解时,定义一个异常的处理方法,加上@ExceptionHandler注解,这个方法就会处理类中其他方法抛出的异常,需要的朋友可以参考下
    2024-01-01
  • Spring Boot超详细分析启动流程

    Spring Boot超详细分析启动流程

    SpringBoot是Spring开源组织下的子项目,是Spring组件一站式解决方案,主要是简化了使用Spring的难度,简省了繁重的配置,提供了各种启动器,开发者能快速上手,这篇文章主要给大家介绍了关于Spring Boot启动流程知识点的相关资料,需要的朋友可以参考下
    2022-07-07
  • Java反射机制的学习总结

    Java反射机制的学习总结

    总的来说,java反射机制是一个很好用的东西,用它可以解决很多死的东西,因为反射机制的灵活行很大,有了他,我们就不要花太多的时间来写操做数据库的代码了,而是方法更多的时间在项目的逻辑功能上,这个可以很大的减少开发时间,而且代码的可读性好
    2013-09-09
  • 详解Spring Bean的配置方式与实例化

    详解Spring Bean的配置方式与实例化

    本文主要带大家一起学习一下Spring Bean的配置方式与实例化,文中的示例代码讲解详细,对我们学习Spring有一定的帮助,需要的可以参考一下
    2022-06-06
  • Springboot+MybatisPlus实现带验证码的登录

    Springboot+MybatisPlus实现带验证码的登录

    本文主要介绍了Springboot+MybatisPlus实现带验证码的登录,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-05-05

最新评论