☰
In this tutorial, we’ll be looking at the HBase Java Admin API examples.
A table in HBase can be created using the createTable() method of HBaseAdmin class. This class belongs to the org.apache.hadoop.hbase.client package. HBaseAdmin class requires the Configuration object as a parameter. Hence instantiate the Configuration class and pass this instance to HBaseAdmin constructor.
Now create an instance of HTableDescriptor class which contains table names and column families. And then using the createTable() method of HBaseAdmin class, create the table.
import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
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;
import org.apache.hadoop.conf.Configuration;
public class CreateTable {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
// instantiating HbaseAdmin class by passing on configuration class
HBaseAdmin admin = new HBaseAdmin(conf);
HTableDescriptor tableDescriptor = new
HTableDescriptor(TableName.valueOf("employee"));
// adding column families to table descriptor
tableDescriptor.addFamily(new HColumnDescriptor("personal"));
tableDescriptor.addFamily(new HColumnDescriptor("employer"));
tableDescriptor.addFamily(new HColumnDescriptor("contactinfo"));
// creating the table
admin.createTable(tableDescriptor);
System.out.println(" Table created ");
}
}
The employee table defined above contains 3 column families: personal, employer and contactinfo. To create a table you create an HTableDescriptor and add one or more column families by adding HColumnDescriptor objects. You then call createTable to create the table.
list is the command that is used to list all the tables in HBase using HBase shell. Similarly we will list the tables using Java api.
You can get the list of all the tables in HBase by calling a method called listTables() in the class HBaseAdmin. This method returns an array of HTableDescriptor objects. And then get the name of the tables from this HTableDescriptor using getNameAsString() method.
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{
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
// getting the list of tables using HBaseAdmin object
HTableDescriptor[] tableDescriptor = admin.listTables();
for (int i=0; i
To verify the existence of a table using HBase shell you execute the exists command. Similarly using Java API, you can call tableExists() method of the HBaseAdmin class to verify the existence of a table in HBase.
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{
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
// verifying the existance of the table
boolean bool = admin.tableExists("employee");
System.out.println("employee table exists: " + bool);
}
}
To disable a table, disableTable() method of the HBaseAdmin class is used.
Its a good programming technique to verify whether a table is disabled or not before we disable it. isTableDisabled() method of the HBaseAdmin class is used to verify whether a table is disabled or not.
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{
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
// verifying whether the table is disabled or not
Boolean isDisabled = admin.isTableDisabled("employee");
// disabling the table
if(!isDisabled){
admin.disableTable("employee");
System.out.println("employee table is disabled");
}
}
}
To enable a table, enableTable() method of the HBaseAdmin class is used.
Again its a good programming technique to verify whether a table is enabled or not before we enable it. isTableEnabled() method of the HBaseAdmin class is used to verify whether a table is disabled or not.
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{
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
// verifying whether the table is enabled or not
Boolean isEnabled = admin.isTableEnabled("employee");
// enabling the table
if(!isEnabled){
admin.enableTable("employee");
System.out.println("employee table is enabled");
}
}
}
You can delete a table using the deleteTable() method in the HBaseAdmin class.
An existing table must be disabled before it can be deleted.
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 {
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
// disabling the table
admin.disableTable("employee");
// deleting employee table
admin.deleteTable("employee");
System.out.println("employee table is deleted");
}
}
Use addColumn() method of HBAseAdmin class to add a column family to a table. The addColumn() method requires a table name and an object of HColumnDescriptor class. Therefore instantiate the HColumnDescriptor class by passing the column family name to the constructor. Below we are adding a column family named 'payRollInfo' to the existing 'employee' table.
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 AddColoumn{
public static void main(String args[]) throws MasterNotRunningException, IOException{
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
// instantiating columnDescriptor
HColumnDescriptor columnDescriptor = new HColumnDescriptor("payRollInfo");
// adding column family
admin.addColumn("employee", columnDescriptor);
System.out.println("payRollInfo coloumn is added");
}
}
Use deleteColumn() method of HBAseAdmin class to delete a column family of a table. The deleteColumn() method requires a 'table name' and an 'column family name' as parameters. Below we are deleting a column family named 'payRollInfo' of 'employee' table.
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 DeleteColoumn{
public static void main(String args[]) throws MasterNotRunningException, IOException{
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
// deleting a column family
admin.deleteColumn("employee","payRollInfo");
System.out.println("payRollInfo coloumn is deleted");
}
}
Instantiate the HBaseAdmin class and call shutdown() method to stop/shut down the HBase.
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 {
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
// Shutting down HBase
System.out.println("Stopping the HBase");
admin.shutdown();
}
}