Support Questions

Find answers, ask questions, and share your expertise

Simple way to create a new table using java in hbase?

avatar
New Contributor

Is there a simple way to create a table with java in a new deploy of ambari hortonworks 2.4? Libs that I need?

1 ACCEPTED SOLUTION

avatar
Super Guru
@Albert Domingo Labernia

CreateHBaseTable.java

import org.apache.hadoop.conf.Configuration;
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;

public class CreateHBaseTable
{
  public static void main(String[] args) throws IOException
  {
    HBaseConfiguration hconfig = new HBaseConfiguration(new Configuration());
    HTableDescriptor htable = new HTableDescriptor("Test"); 
    htable.addFamily( new HColumnDescriptor("id"));
    htable.addFamily( new HColumnDescriptor("Name"));
    System.out.println( "Connecting..." );
    HBaseAdmin hbase_admin = new HBaseAdmin( hconfig );
    System.out.println( "Creating Table..." );
    hbase_admin.createTable( htable );
    System.out.println("Done!");
  }
}
export HADOOP_CLASSPATH= $HADOOP_PATH:`hbase classpath`
 javac -cp `hbase classpath` CreateHBaseTable.java 
 java -cp `hbase classpath` CreateHBaseTable

View solution in original post

5 REPLIES 5

avatar
Master Collaborator

You can find example in refguide (http://hbase.apache.org/book.html#rowkey.regionsplits) :

createTable(Admin admin, HTableDescriptor table, byte[][] splits)

Dependencies include hbase-client jar, hbase-protocol jar

avatar
Master Collaborator

Search for ".createTable(" in http://hbase.apache.org/book.html

Example 32. Java API

avatar
Super Guru
@Albert Domingo Labernia

CreateHBaseTable.java

import org.apache.hadoop.conf.Configuration;
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;

public class CreateHBaseTable
{
  public static void main(String[] args) throws IOException
  {
    HBaseConfiguration hconfig = new HBaseConfiguration(new Configuration());
    HTableDescriptor htable = new HTableDescriptor("Test"); 
    htable.addFamily( new HColumnDescriptor("id"));
    htable.addFamily( new HColumnDescriptor("Name"));
    System.out.println( "Connecting..." );
    HBaseAdmin hbase_admin = new HBaseAdmin( hconfig );
    System.out.println( "Creating Table..." );
    hbase_admin.createTable( htable );
    System.out.println("Done!");
  }
}
export HADOOP_CLASSPATH= $HADOOP_PATH:`hbase classpath`
 javac -cp `hbase classpath` CreateHBaseTable.java 
 java -cp `hbase classpath` CreateHBaseTable

avatar
New Contributor

@Jitendra Yadav Your example works fine, thanks a lot. And tanks for the info too @Ted Yu

avatar
Expert Contributor

Import the below APIs and try in the lines of below:

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.client.Table;

private static final TableName TABLENAME = TableName.valueOf(“NewTable”);

Table table= null;

Configuration config= HBaseConfiguration.create();

Connection connection == ConnectionFactory.createConnection(config);

table = connection.getTable(TABLENAME);