hbase-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mafish Liu" <maf...@gmail.com>
Subject Getting start with hbase-based program.
Date Thu, 28 Feb 2008 06:26:06 GMT
Hi, all:
  I wrote this guild to tell how to start to program with hbase. Please give
a review to
check if there are any errors or improvements.
  Attachment is an example.

Getting start with hbase-based program.

This guild is for those who are fresh to hbase. It can teach you to
get involved with basic hbase operations about in 30 minutes, including:
--connect to hbase
--create/delete tables
--add/read/modify/delete columns
--add/read/modify/delete values

For more details, please refer to hbase java doc. And also, hbase test
example is your good teacher.

== Before Start ==

Before start, be sure that you already have your hadoop and hbase run.
Please follow "hbase in ten minutes" for details.

== Connect to hbase ==
Steps to connect to hbase:
1. Read configuration file.
2. Get HBaseAdmin. HBaseAdmin is the entry to access hbase tables.
    ...
    //configuration file
    HBaseConfiguration hbaseConf = new HBaseConfiguration();
    //connect to hbase.
    HBaseAdmin hbaseAdmin = new HBaseAdmin(hbaseConf);
    ...

== Create tables ==
Steps to create tables:
1. Decide table name.
2. Decide column families.
3. Create tables

        //table name
        Text tableName = new Text(TABLENAME);
        //column family
        Text columnFamily = new Text (COLUMNFAMILY);
        //column descriptor
        HColumnDescriptor column = new HColumnDescriptor(
columnFamily.toString());
        //table descriptor
        HTableDescriptor table = new HTableDescriptor(tableName.toString());
        //add column into table
        table.addFamily(column);

        //create table
        try {
            hbaseAdmin.createTable(table);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

== get all table names ==
Use method listTables() in class org.apache.hadoop.hbase.HBaseAdmin.

== delete table ==
You can delte a table by simply giving a table name.
        hbaseAdmin.deleteTable(table);

== read all column names ==
Step to get all column names:
1. Get table descriptor by table name.
2. Get column family set.

        Set<Text> colNames = null;
        //get table descriptor by table name
        HTableDescriptor tableDesc = null;
        HTableDescriptor[] tableDescs = hbaseAdmin.listTables();
        for (HTableDescriptor t : tableDescs) {
            if (t.getName().toString().equals(TABLENAME))
                tableDesc = t;
        }

        //get all column names
        colNames = tableDesc.getFamilies().keySet();


== add/delete/modify column ==
Please refer to method addColumn(), deleteColumn(), modifyColumn() in
class org.apache.hadoop.hbase.HBaseAdmin.

Note that you need to disable table by call hbaseAdmin.disableTable()
method before the operation and hbaseAdmin.enableTable() after.

== insert/update data in table ==
Step to add data into table:
1. Get HTable by table name.
2. Start atomic insert/update
3. Perform insert/update
4. Commit
        //get HTable by name
        HTable table = new HTable (hbaseConf, TABLENAME);
        //start insert
        long lockid = table.startUpdate(ROW);
        //insert
        byte[] value = VALUE.toString().getBytes("UTF-8");
        table.put(lockid, COLUMNFAMILY1, value);
        //commit
        table.commit(lockid);

== read column values ==

        HashMap<Text, TreeMap<Text,byte[]>> results = new HashMap();

        //get table
        HTable table = new HTable (hbaseConf, TABLENAME);
        //get scanner
        HScannerInterface scanner = table.obtainScanner(new
Text[]{COLUMNFAMILY1}, new Text(""));
        //scan table
        HStoreKey key = new HStoreKey();
        TreeMap<Text, byte[]> resultOfRow = new TreeMap<Text, byte[]>();

        while(scanner.next(key, resultOfRow)) {
            results.put(key.getRow(), resultOfRow);
            key = new HStoreKey();
            resultOfRow = new TreeMap<Text, byte[]>();
        }


-- 
Mafish@gmail.com
Institute of Computing Technology, Chinese Academy of Sciences, Beijing.

Mime
View raw message