Pages

Hbase

Hbase is a distributed column-oriented database built on top of Hadoop Distributed file System. Hbase is the hadoop application to use when you require real-time read and write random-access to very large datasets.

more info : http://hbase.apache.org/

Installation


Download stable release from above page and extract it to somewhere on your file system.
For linux users
                 tar xzf hbase-x.y.z.tar.gz


First You have to set the JAVA_HOME environment variable .

After that set the HBASE_INSTALL environment variable.
For linux users you can edit the bash_profile or simply write the line in your console.
                     export HBASE_INSTALL=/home/hbase/hbase-x.y.z
                     export PATH=$PATH:$HBASE_INSTALL/bin 


To verify your installation check with hbase command. It will generate list of options.

To start a tempory instance of hbase go to your hbase bin dirctory and type  start-hbase.sh.


Now it will start the hbase instance.


To Administer your hbase instance, launch the Hbase shell by typing hbase shell in console. This will bring a interpreter that has some hbase specific commands added to it.
For help just type help in console and you can see the list of help commands.

To  create a table name test with a single column family data
 create 'test','data'
and press enter.

To list down the tables type list command.

To insert data to table
put 'test','row1','data:1','val1'

To get the description of the table.
scan 'test'
 To drop the table first you have to disable it and drop it.
disable 'test'
drop 'test'

Shutdown the hbase instance
stop-hbase.sh


Using Java we can write a programme to retrieve, insert, delete, update data to hbase tables.

First Create the tables as below.
create 'DemoInsert', 'details'


To insert data create class call InsertData and 
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;


public class InsertData {

public static void main(String[] args) throws Exception {


HBaseConfiguration hbaseConfig = new HBaseConfiguration();
HTable htable = new HTable(hbaseConfig, "DemoInsert");
htable.setAutoFlush(false);
htable.setWriteBufferSize(1024 * 1024 * 12);

int totalRecords = 100000;
int maxID = totalRecords / 1000;
Random rand = new Random();
System.out.println("importing " + totalRecords + " records ....");
for (int i=0; i < totalRecords; i++) {
int userID = rand.nextInt(maxID) + 1;
byte [] rowkey = Bytes.add(Bytes.toBytes(userID), Bytes.toBytes(i));
String randomPage =rand.nextInt()*1000 ;
Put put = new Put(rowkey);
put.add(Bytes.toBytes("details"), Bytes.toBytes("page"), Bytes.toBytes(randomPage)); htable.put(put);
}
htable.flushCommits();
htable.close();
System.out.println("done");
} }

This is a basic example to show how to insert data to table in java.

You can find more smiler example in : http://jgray.la/javadoc/hbase-0.20.0/org/apache/hadoop/hbase/client/package-summary.html







 

1 comment: