hbase轻量级中间件simplehbase v0.2简介

[url]https://github.com/zhang-xzhi/simplehbase/[/url]
[url]https://github.com/zhang-xzhi/simplehbase/wiki[/url]
simplehbase简介

simplehbase是java和hbase之间的轻量级中间件。 主要包含以下功能。

数据类型映射:java类型和hbase的bytes之间的数据转换。
简单操作封装:封装了hbase的put,get,scan等操作为简单的java操作方式。
hbase query封装:封装了hbase的filter,可以使用sql-like的方式操作hbase。
动态query封装:类似于myibatis,可以使用xml配置动态语句查询hbase。

simplehbase示例
setup hbase

可以参考网上文档。
初始化simplehbase

private static SimpleHbaseClient getSimpleHbaseClient() {
HBaseDataSource hbaseDataSource = new HBaseDataSource();
List<String> hbaseConfigFilePaths = new ArrayList<String>();
//hbase配置文件。
hbaseConfigFilePaths.add("sample\\hbase_site");
//zk配置文件。
hbaseConfigFilePaths.add("sample\\zk_conf");
hbaseDataSource.setHbaseConfigFilePaths(hbaseConfigFilePaths);
hbaseDataSource.init();

HBaseTableConfig hbaseTableConfig = new HBaseTableConfig();
//simplehbase配置文件。
hbaseTableConfig.setConfigFilePath("sample\\myRecord.xml");
hbaseTableConfig.init();

SimpleHbaseClient tClient = new SimpleHbaseClientImpl();
tClient.setHBaseDataSource(hbaseDataSource);
tClient.setHbaseTableConfig(hbaseTableConfig);

return tClient;
}


simplehbase配置xml

包含htable的配置和一个动态查询的配置

<HBaseTableSchema tableName="MyRecord" defaultFamily="MyRecordFamily">
<HBaseColumnSchema qualifier="id" typeName="int" />
<HBaseColumnSchema qualifier="name" typeName="string" />
<HBaseColumnSchema qualifier="date" typeName="date" />
<HBaseColumnSchema qualifier="gender" typeName="allen.sample.Gender" />
<HBaseColumnSchema qualifier="age" typeName="int" />
</HBaseTableSchema>

<statements>
<statement id="queryByNameAndAge">
select where id greater #id#
<isPropertyAvailable prepend="and" property="name">
name equal #name#
</isPropertyAvailable>
<isPropertyAvailable prepend="and" property="age">
age greater #age#
</isPropertyAvailable>
</statement>
</statements>


定义DO对象

@HBaseTable(defaultFamily = "MyRecordFamily")
public class Person {
@HBaseColumn(qualifier = "id")
private int id;
@HBaseColumn(qualifier = "name")
private String name;
@HBaseColumn(qualifier = "date")
private Date date;
@HBaseColumn(qualifier = "gender")
private Gender gender;
@HBaseColumn(qualifier = "age")
private int age;
}


定义该htable的rowkey

public class PersonRowKey implements RowKey {

private int row;

public PersonRowKey(int row) {
this.row = row;
}

@Override
public byte[] toBytes() {
return Bytes.toBytes(row);
}
}


使用simplehbaseclient操作hbase

public static void main(String[] args) throws Exception {

SimpleHbaseClient simpleHbaseClient = getSimpleHbaseClient();

//插入一条记录。
Person one = new Person();
one.setId(1);
one.setName("allen");
one.setAge(30);
one.setGender(Gender.MALE);
simpleHbaseClient.putObject(new PersonRowKey(1), one);

//插入一条记录。
Person two = new Person();
two.setId(2);
two.setName("dan");
two.setAge(31);
two.setGender(Gender.FEMALE);
simpleHbaseClient.putObject(new PersonRowKey(2), two);

//按照主键查询。
Person result = simpleHbaseClient.findObject(new PersonRowKey(1),
Person.class);
log.info(result);

//按照范围查询
List<Person> resultList = simpleHbaseClient.findObjectList(
new PersonRowKey(1), new PersonRowKey(3), Person.class);
log.info(resultList);

//动态语句查询
Map<String, Object> para = new HashMap<String, Object>();
para.put("id", 0);

resultList = simpleHbaseClient.findObjectList(new PersonRowKey(1),
new PersonRowKey(3), Person.class, "queryByNameAndAge", para);
log.info(resultList);

//动态语句查询
para.put("name", "allen");
para.put("age", 0);
resultList = simpleHbaseClient.findObjectList(new PersonRowKey(1),
new PersonRowKey(3), Person.class, "queryByNameAndAge", para);
log.info(resultList);

//删除批量数据。
simpleHbaseClient.deleteObjectList(new PersonRowKey(0),
new PersonRowKey(100));

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值