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));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/112979.html
標籤:云存儲
下一篇:大資料時代的機遇和挑戰?
