Hadoop集成Spring Springboot

在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

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

可变的地方放进文件中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)添加依赖

org.springframework.data spring-data-hadoop-boot 2.5.0.RELEASE-hadoop25

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

自己熟悉MapReduce,Hive

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值