Apache HBase Admin 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 Admin Java API

HBase is written in java, therefore it provides java API to communicate with HBase. Java API is the fastest way to communicate with HBase.

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/*

Creating a Table Using java API

CreateTable.java

import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.TableName;

public class CreateTable {
	public static void main(String[] args) throws IOException {
		// Instantiating configuration class
		Configuration con = HBaseConfiguration.create();
		// Instantiating HbaseAdmin class
		@SuppressWarnings("deprecation")
		HBaseAdmin admin = new HBaseAdmin(con);
		HTableDescriptor tableDescriptor = new HTableDescriptor(
				TableName.valueOf("emp"));
		// Adding column families to table descriptor
		tableDescriptor.addFamily(new HColumnDescriptor("personal data"));
		tableDescriptor.addFamily(new HColumnDescriptor("professional data"));
		// Execute the table through admin
		admin.createTable(tableDescriptor);
		System.out.println(" Table created ");
		admin.close();
	}
}

Listing Tables Using java API

ListTables.java

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class ListTables {
	public static void main(String args[]) throws MasterNotRunningException,
			IOException {
		// Instantiating a configuration class
		Configuration conf = HBaseConfiguration.create();
		// Instantiating HBaseAdmin class
		@SuppressWarnings("deprecation")
		HBaseAdmin admin = new HBaseAdmin(conf);
		// Getting all the list of tables using HBaseAdmin object
		HTableDescriptor[] tableDescriptor = admin.listTables();
		// printing all the table names.
		for (int i = 0; i < tableDescriptor.length; i++) {
			System.out.println(tableDescriptor[i].getNameAsString());
		}
		admin.close();
	}
}

Disabling Tables Using java API

DisableTable.java

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class DisableTable {
	public static void main(String args[]) throws MasterNotRunningException,
			IOException {
		// Instantiating configuration class
		Configuration conf = HBaseConfiguration.create();
		// Instantiating HBaseAdmin class
		@SuppressWarnings("deprecation")
		HBaseAdmin admin = new HBaseAdmin(conf);
		// Verifying weather the table is disabled
		Boolean bool = admin.isTableDisabled("emp");
		System.out.println(bool);
		// Disabling the table using HBaseAdmin object
		if (!bool) {
			admin.disableTable("emp");
			System.out.println("Table disabled");
		}
		admin.close();
	}
}

Enabling Tables Using java API

EnableTable.java

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class EnableTable {
	public static void main(String args[]) throws MasterNotRunningException,
			IOException {
		// Instantiating configuration class
		Configuration conf = HBaseConfiguration.create();
		// Instantiating HBaseAdmin class
		@SuppressWarnings("deprecation")
		HBaseAdmin admin = new HBaseAdmin(conf);
		// Verifying weather the table is disabled
		Boolean bool = admin.isTableEnabled("emp");
		System.out.println(bool);
		// Disabling the table using HBaseAdmin object
		if (!bool) {
			admin.enableTable("emp");
			System.out.println("Table Enabled");
		}
		admin.close();
	}
}

Adding a column family Using java API

AddColumn.java

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.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class AddColumn {
	public static void main(String args[]) throws MasterNotRunningException,
			IOException {
		// Instantiating configuration class.
		Configuration conf = HBaseConfiguration.create();
		// Instantiating HBaseAdmin class.
		@SuppressWarnings("deprecation")
		HBaseAdmin admin = new HBaseAdmin(conf);
		// Instantiating columnDescriptor class
		HColumnDescriptor columnDescriptor = new HColumnDescriptor(
				"professional data");
		// Adding column family
		admin.addColumn("emp", columnDescriptor);
		System.out.println("coloumn added");
		admin.close();
	}
}

Deleting a column family Using java API

DeleteColumn.java

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class DeleteColumn {
	public static void main(String args[]) throws MasterNotRunningException,
			IOException {
		// Instantiating configuration class.
		Configuration conf = HBaseConfiguration.create();
		// Instantiating HBaseAdmin class.
		@SuppressWarnings("deprecation")
		HBaseAdmin admin = new HBaseAdmin(conf);
		// deleting column family
		admin.deleteColumn("emp","professional data");
		System.out.println("coloumn deleted");
		admin.close();
	}
}

Verifying the existance of Table Using java API

TableExists.java

import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class TableExists {
	public static void main(String args[]) throws IOException {
		// Instantiating configuration class
		Configuration conf = HBaseConfiguration.create();
		// Instantiating HBaseAdmin class
		@SuppressWarnings("deprecation")
		HBaseAdmin admin = new HBaseAdmin(conf);
		// Verifying the existance of the table
		boolean bool = admin.tableExists("emp");
		System.out.println(bool);
		admin.close();
	}
}

Deleting a Table Using java API

DeleteTable.java

import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class DeleteTable {
	public static void main(String[] args) throws IOException {
		// Instantiating configuration class
		Configuration conf = HBaseConfiguration.create();
		// Instantiating HBaseAdmin class
		@SuppressWarnings("deprecation")
		HBaseAdmin admin = new HBaseAdmin(conf);
		// disabling table named emp
		admin.disableTable("emp");
		// Deleting emp
		admin.deleteTable("emp");
		System.out.println("Table deleted");
		admin.close();
	}
}

Stoping HBase Using java API

ShutDownHbase.java

import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class ShutDownHbase {
	public static void main(String args[]) throws IOException {
		// Instantiating configuration class
		Configuration conf = HBaseConfiguration.create();
		// Instantiating HBaseAdmin class
		@SuppressWarnings("deprecation")
		HBaseAdmin admin = new HBaseAdmin(conf);
		// Shutting down HBase
		System.out.println("Shutting down hbase");
		admin.shutdown();
		admin.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 Installation   HBase Pseudo Distributed Mode Installation   HBase Fully Distributed Mode Installation   HBase Shell Usage   HBase CRUD Operation   HBase Client Java API Example