Gre*_*Gus 1 spring hadoop hbase spring-boot
我正在寻找构建一个简单的RESTFull API来访问HBase。我看了Python HappyBase,但我的集群是 kerberised。现在我进入了Spring。
我曾经使用Solr Cloud和Spring 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。
gradle 依赖项:
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')
Run Code Online (Sandbox Code Playgroud)
在 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
Run Code Online (Sandbox Code Playgroud)
类 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...
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
服务类,我们现在可以使用配置的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;
}
}
Run Code Online (Sandbox Code Playgroud)
休息控制器。
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5324 次 |
| 最近记录: |