使用HBase API对数据库的基本操作,包括创建表、查找表数据等。
pom.xml
<!-- Apache HBase Client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.1</version>
</dependency>
application.properties
HBase.nodes=10.xx.xx.43
HBase.maxsize=500000
HBaseConfig.java
@Configuration
public class HBaseConfig {
//HBase相关配置
@Value("${HBase.nodes}")
private String nodes;
@Value("${HBase.maxsize}")
private String maxsize;
@Bean
public HBaseService getHbaseService(){
org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum",nodes );
conf.set("hbase.client.keyvalue.maxsize",maxsize);
return new HBaseService(conf);
}
}
2、业务模拟
HBaseService.java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import .....
/**
* HBase数据库基本操作
*/
public class HBaseService {
private Logger log = LoggerFactory.getLogger(HBaseService.class);
// The administrative API for HBase
// Admin can be used to create, drop, list, enable and disable and
// otherwise modify tables,
// as well as perform other administrative operations.
private Admin admin = null;
private Connection connection = null;
public HBaseService(Configuration conf) {
try {
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
} catch (IOException e) {
log.error("获取HBase连接失败");
}
}
/**
* 创建表
* create