Apache HBase Client Java API Examples

posted on Nov 20th, 2016

Apache HBase

Apache HBase is an open source, non-relational, distributed database modeled after Google's BigTable and is written in Java. It is developed as part of Apache Software Foundation's Apache Hadoop project and runs on top of HDFS (Hadoop Distributed File System), providing BigTable-like capabilities for Hadoop. That is, it provides a fault-tolerant way of storing large quantities of sparse data (small amounts of information caught within a large collection of empty or unimportant data, such as finding the 50 largest items in a group of 2 billion records, or finding the non-zero items representing less than 0.1% of a huge collection).

Pre Requirements

1) A machine with Ubuntu 14.04 LTS operating system.

2) Apache Hadoop pre installed (How to install Hadoop on Ubuntu 14.04)

3) Apache HBase pre installed (How to install HBase on Ubuntu 14.04)

HBase Client java API

This post describes the java client API for HBase that is used to perform CRUD operations on HBase tables (See, how to create tables.). HBase is written in Java and has a Java Native API. Therefore it provides programmatic access to Data Manipulation Language (DML).

Step 1 - Edit $HOME/.bashrc file by adding the java and hadoop path.

$ sudo gedit $HOME/.bashrc

$HOME/.bashrc file. Add the following line

export CLASSPATH=$CLASSPATH:/usr/local/hbase/lib/*

Step 2 Reload your changed $HOME/.bashrc settings

$ source $HOME/.bashrc

Step 3 - Change the directory to /usr/local/hbase

$ cd /usr/local/hbase

Step 4 - To start up the initial HBase cluster.

$ ./start-hbase.sh

Add all HBase libraries to your java project's class path.

/usrlocal/hbase/lib/*

Inserting Data into Table Using java API (See, how to create tables.).

InsertData.java

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 {
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws IOException {
		// Instantiating Configuration class
		Configuration config = HBaseConfiguration.create();
		// Instantiating HTable class
		HTable hTable = new HTable(config, "emp");
		// Instantiating Put class
		// accepts a row name.
		Put p = new Put(Bytes.toBytes("2"));
		// adding values using add() method
		// accepts column family name, qualifier/row name ,value
		p.add(Bytes.toBytes("personal data"), Bytes.toBytes("name"),
				Bytes.toBytes("praveen"));
		p.add(Bytes.toBytes("personal data"), Bytes.toBytes("city"),
				Bytes.toBytes("bangalore"));
		p.add(Bytes.toBytes("professional data"), Bytes.toBytes("designation"),
				Bytes.toBytes("software developer"));
		p.add(Bytes.toBytes("professional data"), Bytes.toBytes("salary"),
				Bytes.toBytes("50000"));
		// Saving the put Instance to the HTable.
		hTable.put(p);
		System.out.println("data inserted");
		// closing HTable
		hTable.close();
	}
}

Updating Data into Table Using java API

UpdateData.java

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 UpdateData {
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws IOException {
		// Instantiating Configuration class
		Configuration config = HBaseConfiguration.create();
		// Instantiating HTable class
		HTable hTable = new HTable(config, "emp");
		// Instantiating Put class
		// accepts a row name
		Put p = new Put(Bytes.toBytes("1"));
		// Updating a cell value
		p.add(Bytes.toBytes("personal data"), Bytes.toBytes("city"),
				Bytes.toBytes("hyderabad"));
		// Saving the put Instance to the HTable.
		hTable.put(p);
		System.out.println("data Updated");
		// closing HTable
		hTable.close();
	}
}

Retrieving Data from Table Using java API

RetriveData.java

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 {
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws IOException, Exception {
		// Instantiating Configuration class
		Configuration config = HBaseConfiguration.create();
		// Instantiating HTable class
		HTable table = new HTable(config, "emp");
		// Instantiating Get class
		Get g = new Get(Bytes.toBytes("2"));
		// Reading the data
		Result result = table.get(g);
		// Reading values from Result class object
		byte[] value = result.getValue(Bytes.toBytes("personal data"),
				Bytes.toBytes("name"));
		byte[] value1 = result.getValue(Bytes.toBytes("personal data"),
				Bytes.toBytes("city"));
		// Printing the values
		String name = Bytes.toString(value);
		String city = Bytes.toString(value1);
		System.out.println("name: " + name +"  "+ "city: " + city);
		table.close();
	}
}

Deleting Data from Table Using java API

DeleteData.java

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 {
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws IOException {
		// Instantiating Configuration class
		Configuration conf = HBaseConfiguration.create();
		// Instantiating HTable class
		HTable table = new HTable(conf, "emp");
		// Instantiating Delete class
		Delete delete = new Delete(Bytes.toBytes("2"));
		delete.deleteColumn(Bytes.toBytes("personal data"), Bytes.toBytes("name"));
		delete.deleteFamily(Bytes.toBytes("professional data"));
		// deleting the data
		table.delete(delete);
		// closing the HTable object
		table.close();
		System.out.println("data deleted...");
	}
}

Scan Data Using java API

ScanTable.java

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.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

public class ScanTable {
	@SuppressWarnings("deprecation")
	public static void main(String args[]) throws IOException {
		// Instantiating Configuration class
		Configuration config = HBaseConfiguration.create();
		// Instantiating HTable class
		HTable table = new HTable(config, "emp");
		// Instantiating the Scan class
		Scan scan = new Scan();
		// Scanning the required columns
		scan.addColumn(Bytes.toBytes("personal data"), Bytes.toBytes("name"));
		scan.addColumn(Bytes.toBytes("personal data"), Bytes.toBytes("city"));
		// Getting the scan result
		ResultScanner scanner = table.getScanner(scan);
		// Reading values from scan result
		for (Result result = scanner.next(); result != null; result = scanner
				.next())
			System.out.println("Found row : " + result);
		// closing the scanner
		scanner.close();
	}
}

Please share this blog post and follow me for latest updates on

facebook             google+             twitter             feedburner

Previous Post                                                                                          Next Post

Labels : HBase Standalone Mode   HBase Pseudo Distributed Mode Installation   HBase Fully Distributed Mode Installation   HBase Shell Usage   HBase CRUD Operation   HBase Admin Java API Examples