https://github.com/zhang-xzhi/simplehbase/
https://github.com/zhang-xzhi/simplehbase/wiki
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));
- }