Learn the HBase Java API

In the previous articles we learnt about Create, update, reading and writing data using shell commands, in similar way by using Java API (application programming interface) model, we can perform all type of table and data operations in HBase.

Let’s see one by one.

1. Schema Creation

In HBase schema can be created or updated using the HBaseAdmin in the Java API.

Example

In the below example we are creating table as “beyondemp” with two column families named “Emp_name” and “sal”.

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.util.Bytes;
public class CreateHbaseTable {
Configuration config = HBaseConfiguration.create();
public void createtable(String name,String[] colfamily) throws MasterNotRunningException,
                                               ZooKeeperConnectionException, IOException
{
HBaseAdmin admin = new HBaseAdmin(config);
HTableDescriptor des = new HTableDescriptor(Bytes.toBytes(name));
for(int i=0;i<colfamily.length;i++){
des.addFamily(new HColumnDescriptor(colfamily[i]));
}
if(admin.tableExists(name)){
System.out.println(“Table already exist”);
}
else{
admin.createTable(des);
System.out.println(“Table: “+name+ ” Sucessfully created”);
}
}
public static void main(String args[]) throws MasterNotRunningException,
                               ZooKeeperConnectionException,IOException{
CreateHbaseTable op = new CreateHbaseTable();
String tablename = “beyondemp”;
String[] familys = {“Emp_name”,“sal”};
op.createtable(tablename, familys);
}
}

Verification

Here will verify the above created table using list command.

hbase>list

2. HTable

Using HTable we can establish connections with the Hbase table.

HTable is an Hbase internal class that represents an Hbase table. It is used to communicate with a single Hbase table.

Example

In this example using Htable we are establishing connection with hbase table called “beyondemp”.

  Configuration conf = HBaseConfiguration.create();

HTable table = new HTable(conf, “beyondemp”);

table.setAutoFlush(false);

table.setWriteBufferSize(2 * 1024 * 1024); // 2 Mb

// … do useful stuff

table.close()

3. Put

Using put we can adds new records into the HBase table.

Example

In this example we are adding new records to “beyondemp” table with column families “Emp_name” and “sal”, where the vaues are “Roy” and 200000 respectively.

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
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 putHbase {
public static void main(String args[]) throws IOException{
//create instance of default hbase configuration object
Configuration config  = HBaseConfiguration.create();
//Get the table instance
HTable table =  new HTable(config, “beyondemp”);
//create the put object
Put put = new Put(Bytes.toBytes(“row-1”));
//Add the column into the column family Emp_name with qualifier name
put.add(Bytes.toBytes(“Emp_name”), Bytes.toBytes(“Employee1”),Bytes.toBytes(“Roy”));
//Add the column into the column family sal with qualifier name
put.add(Bytes.toBytes(“sal”), Bytes.toBytes(“sal_Employee1”), Bytes.toBytes(“200000”));
//insert the put instance to table
table.put(put);
System.out.println(“Values inserted : “);
table.close();
}
}

Verification

Here we are verifying data from “beyondemp” table using the scan command.

hbase(main)> scan ‘beyondemp’

4. Get

Using Get we can fetches a single record from the hbase table.

Example

In this example we are fetching “Emp_name” column family value using Get class object.

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
public class getHbase{
public static void main(String args[]) throws IOException {
Configuration config = HBaseConfiguration.create();
HTable table = new HTable(config,“beyondemp”);
Get get = new Get(Bytes.toBytes(“row-1”));
get.addColumn(Bytes.toBytes(“Emp_name”), Bytes.toBytes(“Emp_name”));
Result result=table.get(get);
byte[] name = result.getValue(Bytes.toBytes(“Emp_name”),Bytes.toBytes(“Emp_name”));
System.out.println(“Name: “ +Bytes.toString(name));
table.close();
}
}

5. Delete

Using Delete we can deletes a single record from hbase table.

Example

In this example we are deleting an entire row where column family name and column qualifier name is “Emp_name”.

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.Result;
public class deleteHbase{
public static void main(String args[])throws IOException{
Configuration config = HBaseConfiguration.create();
HTable table=new HTable(config, “beyondemp”);
Delete del=new Delete(Bytes.toBytes(“row-1”));
del.deleteColumn(Bytes.toBytes(“Emp_name”),Bytes.toBytes(“Emp_name”) );
table.delete(del);
System.out.println(“value-delted”);
table.close();
}
}

Verification

Here we are verifying whether data is removed from “beyondemp” table using the scan command.

hbase(main)> scan ‘beyondemp’

6. Scan

Using Scan we can prints the entire table contents or a particular row value.

Example

In this example we are printing “Emp_name” column family value using scan object.

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Delete;
public class scanHbase {
public static void main(String args[]) throws IOException{
Configuration config = HBaseConfiguration.create();
HTable table = new HTable(config,“beyondemp”);
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(“Emp_name”), Bytes.toBytes(“Emp_name”));
scan.setStartRow(Bytes.toBytes(“row-1”));
//scan.setStartRow(Bytes.toBytes(“row-4”));
ResultScanner result = table.getScanner(scan);
for(Result res:result){
byte[] val = res.getValue(Bytes.toBytes(“Emp_name”), Bytes.toBytes(“Emp_name”));
System.out.println(“Row-value:”+Bytes.toString(val));
}
table.close();
}
}

“That’s all about the HBase Java API, i hope this article is useful for beginners”