使用Hadoop HBASE的Spring Boot REST
我希望构建一个简单的restfullapi来访问HBase。 我查看了Python HappyBase,但是我的集群被kerberised了。现在我进入了春天 我曾经使用Solr-Cloud和Spring-Boot创建简单的API REST Hbase也可以这样做吗?使用Hadoop HBASE的Spring Boot REST,spring,hadoop,spring-boot,hbase,Spring,Hadoop,Spring Boot,Hbase,我希望构建一个简单的restfullapi来访问HBase。 我查看了Python HappyBase,但是我的集群被kerberised了。现在我进入了春天 我曾经使用Solr-Cloud和Spring-Boot创建简单的API REST Hbase也可以这样做吗? 我不知道我是否必须使用春靴“纱线应用程序” => 或者Spring Hadoop。 => 只需要一个非常简单的API 感谢您的帮助。我编写了一个简单的演示项目,介绍如何在没有xml的spring boot restful应用程序中
我不知道我是否必须使用春靴“纱线应用程序” => 或者Spring Hadoop。 => 只需要一个非常简单的API
感谢您的帮助。我编写了一个简单的演示项目,介绍如何在没有xml的spring boot restful应用程序中使用hbase 这个演示主要依赖于spring数据hadoop和hbase客户端 渐变依赖项:
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(无XML!)中配置hbase连接参数:
类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,将配置注入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);
}
}
服务类,我们现在可以使用已配置的HBasetTemplate:
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控制器
@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);
}
}
@RestController
公共类馈线控制器{
@自动连线
私人饲料服务;
@SuppressWarnings({“unchecked”,“rawtypes”})
@PostMapping(value=“/feed/quote”,consumes=“application/json”,products=“application/json”)
公共响应属性saveQuote(@RequestBody Quote){
报价结果=feedService.saveQuote(报价);
返回新的ResponseEntity(结果,new-HttpHeaders(),HttpStatus.OK);
}
}
我编写了一个简单的演示项目,用于在没有xml的spring boot restful应用程序中使用hbase
这个演示主要依赖于spring数据hadoop和hbase客户端
渐变依赖项:
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(无XML!)中配置hbase连接参数:
类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,将配置注入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);
}
}
服务类,我们现在可以使用已配置的HBasetTemplate:
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控制器
@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);
}
}
@RestController
公共类馈线控制器{
@自动连线
私人饲料服务;
@SuppressWarnings({“unchecked”,“rawtypes”})
@PostMapping(value=“/feed/quote”,consumes=“application/json”,products=“application/json”)
公共响应属性saveQuote(@RequestBody Quote){
报价结果=feedService.saveQuote(报价);
返回新的ResponseEntity(结果,new-HttpHeaders(),HttpStatus.OK);
}
}