Gohmz
Gohmz

Reputation: 1286

Spring Boot REST with Hadoop HBASE

I'm looking to build an simple RESTFull API to access into HBase. I looked Python HappyBase, but my cluster is kerberised. Now I'm into Spring.

I used to make simple API REST with Solr Cloud and Spring Boot.

Is it possible to do same with Hbase ?
I have no idea if I have to use Spring Boot 'Yarn App' => https://spring.io/guides/gs/yarn-basic/

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

Just want a really simple API.

Thanks for help.

Upvotes: 1

Views: 6544

Answers (1)

derek.z
derek.z

Reputation: 967

I wrote a simple demo project for using hbase in spring boot restful application without xml.

This demo mainly depends spring-data-hadoop and hbase-client.

gradle dependencies:

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')

Configure the hbase connection parameters in spring boot's application.properties (No 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

class 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 the configurations into the 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);
    }

}

Service class, we can use the configured HbaseTemplate now:

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

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

Upvotes: 5

Related Questions