1、前言
springBoot整合hbase有两种方式:
- 一种是使用spring-boot-starter-hbase,但是这种方式,使用时需要先创建hbase表的实体类和转换类,有点类似jpa,但是对于非关系型数据库,我不是很喜欢这种用法。而且spring-boot-starter-hbase只有一个1.0.0.RELEASE的版本,对于新版hbase的兼容性尚待测试。
- 所以本文介绍的是第二种方式,使用org.apache.hbase提供的工具包,使用的版本是2.1.0
2、引入依赖
<!-- hbase 客户端 -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.1.0</version>
</dependency>
<!-- hbase协处理器 -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-endpoint</artifactId>
<version>2.1.0</version>
</dependency>
3、编写工具类
@Component
public class HbaseUtil {
/**
* The Logger.
*/
Logger logger = LoggerFactory.getLogger(HbaseUtil.class);
/**
* hbase连接对象
*/
private Connection conn;
/**
* hbase zookeeper地址
*/
@Value("${zookeeper.ip}")
private String zookeeper;
/**
* hbase自带聚和协处理器
*/
private String gatherCoprocessor = AggregateImplementation.class.getName();
/**
* 初始化连接
*/
@PostConstruct
private void initConnection() {
Configuration config = getConfig();
try {
//获取连接
conn = ConnectionFactory.createConnection(config);
logger.info("初始化hbase连接");
} catch (Exception e) {
logger.error("初始化失败", e);
}
}
/**
* 获取配置对象
*
* @return
*/
private Configuration getConfig() {
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", zookeeper);
return config;
}
/**
* 获取连接
*/
private Connection getConnection() {
if (conn.isClosed()) {
synchronized (this) {
if (conn.isClosed()) {
initConnection();
}
}
}
return conn;
}
/**
* 获取表连接
*
* @param tableName
* @return
* @throws IOException
*/
private Table getTable(String tableName)
throws IOException {
return getConnection().getTable(TableName.valueOf(tableName));
}
/**
* 获取admin连接
*
* @return
* @throws IOException
*/
private Admin getAdmin()
throws IOException {
return getConnection().getAdmin();
}
/**
* Creat table boolean.创建表
*
* @param tableName the table name表名
* @param columnFamily the column family列族名的集合
* @return the boolean
*/
public boolean creatTable(String tableName, List<String> columnFamily) {
TableName table = TableName.valueOf(tableName);
try (Admin admin = getAdmin();) {
if (!admin.tableExists(table)) {
TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(table);
for (String s : columnFamily) {
tableDescriptor.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(s)).build());
}
admin.createTable(tableDescriptor.build());
}
} catch (Exception e) {
logger.error("创建表失败", e);
}
return true;
}
/**
* Gets all table names.获取所有表名
*
* @return the all table names
*/
public List<String> getAllTableNames() {
List<String> result = new ArrayList<>();
try (Admin admin = getAdmin();) {
TableName[] tableNames = admin.listTableNames();
for (TableName tableName : tableNames) {
result.add(tableName.getNameAsString());
}
} catch (Exception e) {
logger.error("获取所有表的表名失败", e);
}
return result;
}
/**
* Delete table boolean.删除表
*
* @param tableName the table name要删除的表名
* @return the boolean
*/
public boolean deleteTable(String tableName) {
try (Admin admin = getAdmin();) {
if (admin.tableExists(TableName