In this post, we will be creating a sample HBase project in Eclipse IDE. It is required to have a local working instance of HBase (mine is 0.20.6 version) and Eclipse Helios version. I assume that you are aware of starting and stopping HBase.
Step 1 : Start HBase. Start Eclispe.Step 2 : Create a new Java Project (File -> New -> Java Project).
Step 3 : Fill up the form. (Here project name = Sample) and click next.
Step 4 : Select Libraries tag from it and click “Add External JARs”.
Step 5 : Browse to theĀ $HBASE_HOME directory. ($HBASE_HOME is the directory where you have installed HBase).
Step 6 : Select hbase-0.20.6.jar and click Ok. Similarly add the (commons-logging-1.0.4.jar , log4j-1.2.15.jar, zookeeper-3.2.2.jar, hadoop-0.20.2-core.jar) present in $HBASE_HOME/lib folder and click Finish.Step 7: Create a class with main method and replace the contents with the code displayed below. The below code creates a sample table named Blogposts with 2 column family Author, Title
// HBaseTest.java
import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class HBaseTest {
public static void main(String[] args) {
// Try to create a Table with 2 column family (Title, Author)
HTableDescriptor descriptor = new HTableDescriptor(“blogposts”);
descriptor.addFamily(new HColumnDescriptor(“Title”));
descriptor.addFamily(new HColumnDescriptor(“Author”));
try {
// Create a HBaseAdmin
HBaseConfiguration config = new HBaseConfiguration();
HBaseAdmin admin = new HBaseAdmin(config);
// Create table
admin.createTable(descriptor);
System.out.println(“Table created…”);
} catch (IOException e) {
System.out.println(“IOError: cannot create Table.”);
e.printStackTrace();
}
}
}
Step 8 : Right click project in Package Explorer. Select Run as -> Java Application
Step 9: Check the output screen for any Exceptions, if not thats it.
Step 10: Confirm creation of the table using HBase Shell.
Thats it for now.. will create more posts on HBase soon š