Hadoop和Springboot集成

2020-07-27  本文已影响0人  神豪VS勇士赢

1.引入依赖

    <!--添加spring boot 依赖-->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-hadoop-boot</artifactId>
      <version>RELEASE</version>
    </dependency>
  1. 便携式配置类 注入 FsShell 可以快速操作
package com.imooc.hadoop.spring;
/*
 * Created by zsh
 */

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;

/**
 * 使用Spring Boot访问HDFS
 */
@SpringBootApplication
public class SpringBootHDFSApp implements CommandLineRunner {

    @Autowired
    FsShell fsShell;


    @Override
    public void run(String... strings) throws Exception {

        System.out.println("=========start===========");

        for (FileStatus fileStatus : fsShell.lsr("/springhdfs")) {
            System.out.println("> " + fileStatus.getPath());
        }

        System.out.println("=========end===========");

    }

    public static void main(String args[]) {
        SpringApplication.run(SpringBootHDFSApp.class ,args);
    }

}
  1. 详细看文档
    https://spring.io/projects/spring-hadoop
image.png
上一篇下一篇

猜你喜欢

热点阅读