HBase Client java API








HBase Client java API

This post describes the java client API for HBase that is used to perform CRUD operations on HBase tables

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

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("nagaraju"));
		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();
	}
}

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *