带有 Hadoop HBASE 的 Spring Boot REST

标签 spring hadoop spring-boot hbase

我希望构建一个简单的 RESTFull API 来访问 HBase。 我查看了 Python HappyBase,但我的集群是基于 Kerberos 的。现在我进入了 Spring

我曾经使用 Solr CloudSpring Boot 制作简单的 API REST。

是否可以对 Hbase 做同样的事情?
我不知道是否必须使用 Spring Boot 'Yarn App' => https://spring.io/guides/gs/yarn-basic/

Spring Hadoop。 => https://projects.spring.io/spring-hadoop/

只需要一个非常简单的 API。

感谢您的帮助。

最佳答案

我写了一个简单的演示项目,用于在没有 xml 的 spring boot restful 应用程序中使用 hbase。

本demo主要依赖spring-data-hadoop和hbase-client。

梯度依赖:

compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-web')
compile 'org.springframework.data:spring-data-hadoop:2.5.0.RELEASE'
compile('org.apache.hbase:hbase-client:1.3.1'){
    exclude group :'log4j',module:'log4j'
    exclude group :'org.slf4j',module:'slf4j-log4j12'
    exclude group: 'javax.servlet', module: 'servlet-api'
}
compile('org.springframework.boot:spring-boot-configuration-processor')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')

在spring boot的application.properties中配置hbase连接参数(无XML!):

spring.data.hbase.zkQuorum=192.168.0.109:2181
spring.data.hbase.zkBasePath=/hbase
spring.data.hbase.rootDir=file:///home/hbase-1.2.2

HbaseProperties.java 类:

@ConfigurationProperties(prefix = "spring.data.hbase")
public class HbaseProperties {
    // Addresses of all registered ZK servers.
    private String zkQuorum;

    // Location of HBase home directory
    private String rootDir;

    // Root node of this cluster in ZK.
    private String zkBasePath;

    // getters and setters...

}

HbaseConfig.java,将配置注入(inject)HbaseTemplate:

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.hadoop.hbase.HbaseTemplate;

@Configuration
@EnableConfigurationProperties(HbaseProperties.class)
public class HbaseConfig {

    @Autowired
    private HbaseProperties hbaseProperties;

    @Bean
    public HbaseTemplate hbaseTemplate() {
        org.apache.hadoop.conf.Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", this.hbaseProperties.getZkQuorum());
        configuration.set("hbase.rootdir", this.hbaseProperties.getRootDir());
        configuration.set("zookeeper.znode.parent", this.hbaseProperties.getZkBasePath());
        return new HbaseTemplate(configuration);
    }

}

服务类,我们现在可以使用配置好的HbaseTemplate:

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.stereotype.Service;

import com.zql.hbasedemo.vo.Quote;

@Service
public class FeedService {
    @Autowired
    private HbaseTemplate hbaseTemplate;

    @PostConstruct
    public void test(){
        Quote quote = new Quote();
        quote.setEventType("ft");
        quote.setHandicap("4");
        quote.setMarket("OU");
        quote.setMatchId("27350208");
        quote.setSelection("OVER");
        quote.setPrice("1.93");
        saveQuote(quote);
    }

    public Quote saveQuote(Quote quote) {
        hbaseTemplate.put("quotes", quote.getMatchId(), "data", quote.getMarket() + ":" + quote.getSelection(),
            quote.getPrice().getBytes());
        return quote;
    }
}

休息 Controller 。

@RestController
public class FeedController {
    @Autowired
    private FeedService feedService;

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @PostMapping(value = "/feed/quote", consumes = "application/json", produces = "application/json")
    public ResponseEntity<Quote> saveQuote(@RequestBody Quote quote) {
        Quote result = feedService.saveQuote(quote);
        return new ResponseEntity(result, new HttpHeaders(), HttpStatus.OK);
    }
}

关于带有 Hadoop HBASE 的 Spring Boot REST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47261564/

相关文章:

java - 基本身份验证在 Spring-Boot WS Soap 服务中不起作用

java - 更新多对多关系以包含新字段

java - 添加 @ComponentScan 从 jar 加载 bean 后,我的 Controller 未扫描,并且收到 404

java - Heroku 编译 fatal error : invalid target release: 11

java - org.springframework.web.servlet.PageNotFound noHandlerFound 未找到带有 URI 的 HTTP 请求的映射。我已将项目部署为 Maven 项目

java - 在 hadoop-examples jar 文件上运行 wordcount 时出现 "Not a valid JAR"

hadoop - 在 spark 中使用 hadoop 配置连接到 Hbase

hadoop - hive 中的 avroserde 是否允许更新或删除记录?

java - 在 Spring Boot 应用程序 @RestController 中获取信息的哪种模式更好以及如何指定 ANY @Mapping?

java - Spring Boot如何在发送响应之前修改实体中的列表