前言
最近因为业务关系,用到了Hbase,因为用的是Spring boot框架 ,所以自然而然就用到了spring封装的HbaseTemplate工具类。然而HbaseTemplate封装的代码实在比较糟糕,出了一些基本的CRUD操作之外并没有给我们提供太多便利之处。先来看看痛处:
痛处一:
- 最基本的查询操作(以下只是demo演示):
class UserInfo{
string name;
string password;
}
public void putUserInfo(UserInfo userInfo) {
hBaseTemplate.execute(TABLE_NAME, (table) -> {
//根据rowKey定义一个put对象,可用作插入和更新
Put put = new Put(Bytes.toBytes(rowKey));
//name是否为空
if(userInfo.name!=null){
put.addColumn(COLUMN_FAMILY_NAME.getBytes(), Bytes.toBytes(COLUMN_RAW_DATA),Bytes.toBytes(userInfo.name));
}
//password是否为空
if(userInfo.password!=null){
put.addColumn(COLUMN_FAMILY_NAME.getBytes(), Bytes.toBytes(COLUMN_RAW_DATA),Bytes.toBytes(userInfo.password));
}
table.put(put);
return true;
});
}
相信大家也看出来了,如果待插入的对象有很多字段呢?还要逐个写if语句来判读非空么?明显违背了Java8大力提倡的代码简洁之道。于是,个人封装了一个插入更新模版类(简单的对Put对象的一个扩展):
//继承并扩展Put对象
public class PutExtension extends Put {
String columnFamilyName = "demo";
public PutExtension(String columnFamilyName, byte[] row) {
super(row);
this.columnFamilyName = columnFamilyName;
}
public