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

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 

Java代码   收藏代码
  1. private static SimpleHbaseClient getSimpleHbaseClient() {  
  2.     HBaseDataSource hbaseDataSource = new HBaseDataSource();  
  3.     List<String> hbaseConfigFilePaths = new ArrayList<String>();  
  4.     //hbase配置文件。  
  5.     hbaseConfigFilePaths.add("sample\\hbase_site");  
  6.     //zk配置文件。  
  7.     hbaseConfigFilePaths.add("sample\\zk_conf");  
  8.     hbaseDataSource.setHbaseConfigFilePaths(hbaseConfigFilePaths);  
  9.     hbaseDataSource.init();  
  10.   
  11.     HBaseTableConfig hbaseTableConfig = new HBaseTableConfig();  
  12.     //simplehbase配置文件。  
  13.     hbaseTableConfig.setConfigFilePath("sample\\myRecord.xml");  
  14.     hbaseTableConfig.init();  
  15.   
  16.     SimpleHbaseClient tClient = new SimpleHbaseClientImpl();  
  17.     tClient.setHBaseDataSource(hbaseDataSource);  
  18.     tClient.setHbaseTableConfig(hbaseTableConfig);  
  19.   
  20.     return tClient;  
  21. }  



simplehbase配置xml 

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

Java代码   收藏代码
  1. <HBaseTableSchema tableName="MyRecord" defaultFamily="MyRecordFamily">  
  2.     <HBaseColumnSchema qualifier="id" typeName="int" />  
  3.     <HBaseColumnSchema qualifier="name" typeName="string" />  
  4.     <HBaseColumnSchema qualifier="date" typeName="date" />  
  5.     <HBaseColumnSchema qualifier="gender" typeName="allen.sample.Gender" />  
  6.     <HBaseColumnSchema qualifier="age" typeName="int" />  
  7. </HBaseTableSchema>  
  8.   
  9. <statements>      
  10.     <statement id="queryByNameAndAge">  
  11.         select where id greater #id#  
  12.         <isPropertyAvailable prepend="and" property="name">  
  13.             name equal #name#  
  14.         </isPropertyAvailable>  
  15.         <isPropertyAvailable prepend="and" property="age">  
  16.             age greater #age#  
  17.         </isPropertyAvailable>  
  18.     </statement>          
  19. </statements>   

  

定义DO对象 

Java代码   收藏代码
  1. @HBaseTable(defaultFamily = "MyRecordFamily")  
  2. public class Person {  
  3.     @HBaseColumn(qualifier = "id")  
  4.     private int    id;  
  5.     @HBaseColumn(qualifier = "name")  
  6.     private String name;  
  7.     @HBaseColumn(qualifier = "date")  
  8.     private Date   date;  
  9.     @HBaseColumn(qualifier = "gender")  
  10.     private Gender gender;  
  11.     @HBaseColumn(qualifier = "age")  
  12.     private int    age;  
  13. }  



定义该htable的rowkey 

Java代码   收藏代码
  1. public class PersonRowKey implements RowKey {  
  2.   
  3.     private int row;  
  4.   
  5.     public PersonRowKey(int row) {  
  6.         this.row = row;  
  7.     }  
  8.   
  9.     @Override  
  10.     public byte[] toBytes() {  
  11.         return Bytes.toBytes(row);  
  12.     }  
  13. }  



使用simplehbaseclient操作hbase 

Java代码   收藏代码
  1. public static void main(String[] args) throws Exception {  
  2.   
  3.     SimpleHbaseClient simpleHbaseClient = getSimpleHbaseClient();  
  4.   
  5.     //插入一条记录。  
  6.     Person one = new Person();  
  7.     one.setId(1);  
  8.     one.setName("allen");  
  9.     one.setAge(30);  
  10.     one.setGender(Gender.MALE);  
  11.     simpleHbaseClient.putObject(new PersonRowKey(1), one);  
  12.   
  13.     //插入一条记录。  
  14.     Person two = new Person();  
  15.     two.setId(2);  
  16.     two.setName("dan");  
  17.     two.setAge(31);  
  18.     two.setGender(Gender.FEMALE);  
  19.     simpleHbaseClient.putObject(new PersonRowKey(2), two);  
  20.   
  21.     //按照主键查询。  
  22.     Person result = simpleHbaseClient.findObject(new PersonRowKey(1),  
  23.             Person.class);  
  24.     log.info(result);  
  25.   
  26.     //按照范围查询  
  27.     List<Person> resultList = simpleHbaseClient.findObjectList(  
  28.             new PersonRowKey(1), new PersonRowKey(3), Person.class);  
  29.     log.info(resultList);  
  30.   
  31.     //动态语句查询  
  32.     Map<String, Object> para = new HashMap<String, Object>();  
  33.     para.put("id"0);  
  34.   
  35.     resultList = simpleHbaseClient.findObjectList(new PersonRowKey(1),  
  36.             new PersonRowKey(3), Person.class"queryByNameAndAge", para);  
  37.     log.info(resultList);  
  38.   
  39.     //动态语句查询  
  40.     para.put("name""allen");  
  41.     para.put("age"0);  
  42.     resultList = simpleHbaseClient.findObjectList(new PersonRowKey(1),  
  43.             new PersonRowKey(3), Person.class"queryByNameAndAge", para);  
  44.     log.info(resultList);  
  45.   
  46.     //删除批量数据。  
  47.     simpleHbaseClient.deleteObjectList(new PersonRowKey(0),  
  48.             new PersonRowKey(100));  
  49.   
  50. }  

你可能感兴趣的:(simple)