BIG
DATA

JAVA

Apache HBase - Java Client API examples

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

In this tutorial, we’ll be looking at the HBase Java Client API examples. HBase is written in Java and provides Java API to communicate with it. The client APIs provide both DDL (data definition language) and DML (data manipulation language) semantics very much like what you find in SQL for relational databases. So lets learn to perform CRUD operations on HBase tables using java client API for HBase.

Insert Data Into HBase Table Using Java API

Use the add() method of the Put class to insert data into Hbase. Then to save it use the put() method of the HTable class. To summarize, steps to insert data in HBase table are:

  • Instantiate the Configuration Class
  • Instantiate the HTable Class
  • Instantiate the Put Class
  • Insert and save the Data in Table
  • Close the HTable Instance

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 InsertData{

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

      // instantiate Configuration class
      Configuration config = HBaseConfiguration.create();

      // instantiate HTable class
      HTable hTable = new HTable(config, "employee");

      // instantiate Put class
      Put p = new Put(Bytes.toBytes("row2001")); 

      // add values using add() method
      p.add(Bytes.toBytes("personal"),
		Bytes.toBytes("name"),Bytes.toBytes("Vivek"));
      p.add(Bytes.toBytes("personal"),
		Bytes.toBytes("age"),Bytes.toBytes("17"));
      p.add(Bytes.toBytes("contactinfo"),Bytes.toBytes("city"),
		Bytes.toBytes("Bengaluru"));
      p.add(Bytes.toBytes("contactinfo"),Bytes.toBytes("country"),
		Bytes.toBytes("India"));
      p.add(Bytes.toBytes("contactinfo"),Bytes.toBytes("email"),
		Bytes.toBytes("[email protected]"));
      
      // save the put Instance to the HTable.
      hTable.put(p);
      System.out.println("data inserted successfully");
      
      // close HTable instance
      hTable.close();
   }
}

Note: To instantiate the HTable Class, constructor accepts 'configuration object' and 'table name' as parameters.

Note: To instantiate the Put Class, constructor accepts 'row name' you want to insert the data into, in string format.

Note: To insert data the add() method of Put class is used which requires 3 byte arrays representing 'column family', 'column name', and the 'value' to be inserted, respectively.

Note: Once data is inserted, save the changes by adding the 'put' instance to the put() method of HTable class


Reading Data from HBase Table Using Java API

The get() method of the HTable class can be used to read data from Hbase table. The get() method requires an instance of the Get class. To summarize, steps to read data from HBase table are:

  • Instantiate the Configuration Class
  • Instantiate the HTable Class
  • Instantiate the Get Class
  • Get the Result object by passing above Get class instance
  • Read Values from the above Result Instance
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

public class RetriveData{

   public static void main(String[] args) throws IOException, Exception{
   
      Configuration config = HBaseConfiguration.create();
      HTable table = new HTable(config, "employee");

      // instantiate Get class
      Get g = new Get(Bytes.toBytes("row2001"));

      // get the Result object
      Result result = table.get(g);

      // read values from Result class object
      byte [] name = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("name"));
      byte [] age = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("age"));
      byte [] city = result.getValue(Bytes.toBytes("contactinfo"),Bytes.toBytes("city"));
      byte [] country = result.getValue(Bytes.toBytes("contactinfo"),Bytes.toBytes("country"));
      byte [] email = result.getValue(Bytes.toBytes("contactinfo"),Bytes.toBytes("email"));
      
      System.out.println("name: " + Bytes.toString(name));
      System.out.println("age: " + Bytes.toString(age));
      System.out.println("city: " + Bytes.toString(city));
      System.out.println("country: " + Bytes.toString(country));
      System.out.println("email: " + Bytes.toString(email));
   }
}

Updating Data in HBase table Using Java API

You can update the data in a particular cell using the put() method.

Steps are same as inserting data into HBase table using Java API.

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 InsertData{

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

      // instantiate Configuration class
      Configuration config = HBaseConfiguration.create();

      // instantiate HTable class
      HTable hTable = new HTable(config, "employee");

      // instantiate Put class
      Put p = new Put(Bytes.toBytes("row2001")); 

      // update value using add() method
      p.add(Bytes.toBytes("personal"),
		Bytes.toBytes("age"),Bytes.toBytes("23"));
      
      // save the put Instance to the HTable.
      hTable.put(p);
      System.out.println("data updated successfully");
      
      hTable.close();
   }
}


Deleting Data From HBase Table Using Java API

The delete() method of the HTable class can be used to delete data from Hbase table. The delete() method requires an instance of the Delete class. To summarize, steps to delete data from HBase table are:

  • Instantiate the Configuration Class
  • Instantiate the HTable Class
  • Instantiate the Delete Class
  • Delete the Data by passing Delete instance to delete() method of HTable
  • Close the HTable Instance
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;

public class DeleteData {

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

      Configuration conf = HBaseConfiguration.create();
      HTable table = new HTable(conf, "employee");

      // instantiate Delete class
      Delete delete = new Delete(Bytes.toBytes("row2001"));
      delete.deleteColumn(Bytes.toBytes("personal"), Bytes.toBytes("age"));
      delete.deleteFamily(Bytes.toBytes("contactinfo"));

      // delete the data
      table.delete(delete);

      table.close();
      System.out.println("data deleted successfully.....");
   }
}


Scan The HBase Table Using Java API

The getScanner() method of the HTable class can be used to scan the entire data of Hbase table. The getScanner() method expects an instance of the Scan class. To summarize, steps to scan the entire data of HBase table are:

  • Instantiate the Configuration Class
  • Instantiate the HTable Class
  • Instantiate the Scan Class
  • Get the ResultScanner instances by calling HTable's getScanner() method
  • Read values from ResultScanner and close it
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;

public class ScanTable{

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

      Configuration config = HBaseConfiguration.create();
      HTable table = new HTable(config, "employee");

      // instantiate the Scan class
      Scan scan = new Scan();

      // scan the columns
      scan.addColumn(Bytes.toBytes("personal"), Bytes.toBytes("name"));
      scan.addColumn(Bytes.toBytes("personal"), Bytes.toBytes("age"));

      // get the ResultScanner
      ResultScanner scanner = table.getScanner(scan);
      for (Result result = scanner.next(); result != null; result=scanner.next())
        System.out.println("Found row : " + result);

      scanner.close();
   }
}