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[]>();
        }


-- 
[EMAIL PROTECTED]
Institute of Computing Technology, Chinese Academy of Sciences, Beijing.
package cn.ac.ict.hla;


import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeMap;

import org.apache.hadoop.hbase.HBaseAdmin;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HScannerInterface;
import org.apache.hadoop.hbase.HStoreKey;
import org.apache.hadoop.hbase.HTable;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.io.Text;

/**
 * This class demonstrates the basic hbase operations, including:
 * --connect to hbase
 * --create/delete tables
 * --insert/modify/delete columns 
 * --insert/modify/delete values
 * @author [EMAIL PROTECTED]
 *
 */
public class HTableTest {
	//table name
	public static final Text TABLENAME = new Text("testtable");
	//column family 1
	public static final Text COLUMNFAMILY1= new Text ("testinfo1:");
	//column family 2
	public static final Text COLUMNFAMILY2 = new Text("testinfo2:");
	//row
	public static final Text ROW = new Text("row");
	//value
	public static final Text VALUE = new Text("value");
	/** read configuration file*/
	private HBaseConfiguration hbaseConf = null;
	/** HBaseAdmin is the key to access hbase tables */
	private HBaseAdmin hbaseAdmin = null;
	
	/**
	 * connect to hbase master.
	 */
	public HTableTest () {
		this.hbaseConf = new HBaseConfiguration ();
		try {
			//connect to hbase master.
			this.hbaseAdmin = new HBaseAdmin(hbaseConf);
		} catch (MasterNotRunningException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @return HBaseAdmin 
	 */
	public HBaseAdmin getAdmin () {
		return this.hbaseAdmin;
	}
	
	/**
	 *  create table which is named TABLENAME with conlumn COLUMNFAMILY1
	 * @throws IOException
	 */
	public void createTable() throws IOException{

		//column descriptor
		HColumnDescriptor column = new HColumnDescriptor(COLUMNFAMILY1.toString());
		//table descriptor
		HTableDescriptor table = new HTableDescriptor(TABLENAME.toString());
		//add column into table
		table.addFamily(column);
		//create table
		createTable(table);
	}
	
	/**
	 * create table
	 * @param tableDes table descriptor
	 * @throws IOException
	 */
	public void createTable(HTableDescriptor tableDes) throws IOException {
		this.hbaseAdmin.createTable(tableDes);
	}
	
	/**
	 * delete table
	 * @throws IOException
	 */
	public void deleteTable() throws IOException{
		this.hbaseAdmin.deleteTable(TABLENAME);
	}
	
	/**
	 *  add a column family
	 * @throws IOException
	 */
	public void addColumn() throws IOException {
		this.hbaseAdmin.disableTable(TABLENAME);
		HColumnDescriptor colDesc = new HColumnDescriptor (COLUMNFAMILY2.toString());
		this.hbaseAdmin.addColumn(TABLENAME, colDesc);
		this.hbaseAdmin.enableTable(TABLENAME);
		
	}
	
	/**
	 * delete a column family
	 * @throws IOException
	 */
	public void deleteColumn() throws IOException{
		this.hbaseAdmin.disableTable(TABLENAME);
		this.hbaseAdmin.deleteColumn(TABLENAME, COLUMNFAMILY1);
		this.hbaseAdmin.enableTable(TABLENAME);
	}
	
	/**
	 * modify column family name from COLUMNFAMILY1 to COLUMNFAMILY2
	 * @throws IOException
	 */
	public void modifyColumn() throws IOException {
		this.hbaseAdmin.disableTable(TABLENAME);
		HColumnDescriptor colDesc = new HColumnDescriptor(COLUMNFAMILY1.toString());
		this.hbaseAdmin.modifyColumn(TABLENAME, COLUMNFAMILY2, colDesc);
		this.hbaseAdmin.enableTable(TABLENAME);
	}
	
	/**
	 * read all column family names
	 * @return A Set<Text> with family names. 
	 * @throws IOException
	 */
	public Set<Text> readAllColNames() throws IOException {
		Set<Text> colNames = null;
		//get table descriptor
		HTableDescriptor tableDesc = null;
		HTableDescriptor[] tableDescs = hbaseAdmin.listTables();
		for (HTableDescriptor t : tableDescs) {
			if (t.getName().toString().equals(TABLENAME.toString())) 
				tableDesc = t;
			else {
				throw new IOException("No such table:" + TABLENAME);
			}
		}
		
		//get all column names
		colNames = tableDesc.getFamilies().keySet();
		return colNames;
	}
	
	/**
	 * add data into table.
	 * @throws IOException
	 */
	public void addDataIntoTable() throws IOException {
		//get HTable by name
		HTable table = new HTable (hbaseConf, TABLENAME);
		//start insert values into row "row".
		//if you want to insert/update multiple rows, 
		//remember give different row name for each row.
		long lockid = table.startUpdate(ROW);
		//insert
		byte[] value = VALUE.toString().getBytes("UTF-8");
		table.put(lockid, COLUMNFAMILY1, value);
		//commit
		table.commit(lockid);
	}
	
	/**
	 * read all value from table TABLENAME
	 * @return a HashMap of row->(column->value)
	 * @throws IOException
	 */
	public HashMap<Text, TreeMap<Text,byte[]>>  getAllValues () throws IOException {
		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[]>();
		}
		return results;	
	}
}

Reply via email to