hbase-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhang_xzhi <zhang_x...@163.com>
Subject a short introduction of simplehbase(hbase ORM)
Date Sun, 05 Jan 2014 15:58:22 GMT
https://github.com/zhang-xzhi/simplehbase
https://github.com/zhang-xzhi/simplehbase/wiki

I am writing a light weight hbase ORM now, search for some suggestion.
thanks. 


# 3 mins on simplehbase

## Introduction to simplehbase
Simplehbase is a lightweight ORM framework between java app and hbase.
The main feature of it are following: 
* data type mapping: mapping java type to hbase's bytes back and forth.
* hbase operation wrapping: warpping hbase's put get scan operation to
simple java interface.
* hbase query language: using hbase filter, simplehbase can use sql-like
style to operate on hbase.
* dynamic query: like myibatis, simplehbase can use xml config file to
define dynamic query to operate on hbase.
* insert update support: provide insert, update on top of checkAndPut.
* multiple version support: provide interface to operation on hbase's
multiple version.
* hbase native interface support.

## Simplehbase sample

### Init simplehbase
    private static SimpleHbaseClient getSimpleHbaseClient() {
        HBaseDataSource hbaseDataSource = new HBaseDataSource();
        List<String> hbaseConfigFilePaths = new ArrayList<String>();
        //hbase config file.
        hbaseConfigFilePaths.add("sample\\hbase_site");
        //zk config file.
        hbaseConfigFilePaths.add("sample\\zk_conf");
        hbaseDataSource.setHbaseConfigFilePaths(hbaseConfigFilePaths);
        hbaseDataSource.init();

        HBaseTableConfig hbaseTableConfig = new HBaseTableConfig();
        //simplehbase config file.
        hbaseTableConfig.setConfigFilePath("sample\\myRecord.xml");
        hbaseTableConfig.init();

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

        return tClient;
    }
### Simplehbase config xml 
including htable's config and one dynamic query config.
<SimpleHbase>

	<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>	
</SimpleHbase>   

### Define Data Object
    @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;
    }

### Define RowKey of Data Object
	public class PersonRowKey implements RowKey {
	
		private int row;
	
		public PersonRowKey(int row) {
			this.row = row;
		}
	
		@Override
		public byte[] toBytes() {
			return Bytes.toBytes(row);
		}
	}
### Using SimpleHbaseClient to operate hbase
    public static void main(String[] args) throws Exception {

        SimpleHbaseClient simpleHbaseClient = getSimpleHbaseClient();

        //insert one record.
        Person one = new Person();
        one.setId(1);
        one.setName("allen");
        one.setAge(30);
        one.setGender(Gender.MALE);
        simpleHbaseClient.putObject(new PersonRowKey(1), one);

        //insert another record.
        Person two = new Person();
        two.setId(2);
        two.setName("dan");
        two.setAge(31);
        two.setGender(Gender.FEMALE);
        simpleHbaseClient.putObject(new PersonRowKey(2), two);

        //search by rowkey.
        Person result = simpleHbaseClient.findObject(new PersonRowKey(1),
                Person.class);
        log.info(result);

        //search by range.
        List<Person> resultList = simpleHbaseClient.findObjectList(
                new PersonRowKey(1), new PersonRowKey(3), Person.class);
        log.info(resultList);

        //search by dynamic query.
        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);

        //search by dynamic query.
        para.put("name", "allen");
        para.put("age", 0);
        resultList = simpleHbaseClient.findObjectList(new PersonRowKey(1),
                new PersonRowKey(3), Person.class, "queryByNameAndAge",
para);
        log.info(resultList);

        //batch delete.
        simpleHbaseClient.deleteObjectList(new PersonRowKey(0),
                new PersonRowKey(100));

    }








--
View this message in context: http://apache-hbase.679495.n3.nabble.com/a-short-introduction-of-simplehbase-hbase-ORM-tp4054488.html
Sent from the HBase User mailing list archive at Nabble.com.

Mime
View raw message