Support Questions
Find answers, ask questions, and share your expertise

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

Solved Go to solution
Highlighted

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

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

Accepted Solutions
Highlighted

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

@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
Highlighted

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

Super 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

Highlighted

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

Super Collaborator

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

Example 32. Java API

Highlighted

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

@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

Highlighted

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

New Contributor

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

Highlighted

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

Rising Star

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);