Thursday, 2 May 2013

HBase: create table using HBaseAdmin API


In HBase we can create table from HBase shell as follows:

hbase(main):005:0> create 'table_name','column_family_name'

However, you can also create table using HBaseAdmin API as follows:

public class AdminCreate {
    public static void main(String[] args) throws IOException{
        
    
    Configuration conf = HBaseConfiguration.create();
    HBaseAdmin admin = null;
    try{
        admin=new HBaseAdmin(conf);
       }
    catch(Exception e)
       {
        e.printStackTrace();
        
       }
    
    HTableDescriptor tabdesc = new HTableDescriptor("newtable");
    HColumnDescriptor colfam=new HColumnDescriptor("colfam");
    tabdesc.addFamily(colfam);
    
    admin.createTable(tabdesc);
    
    if(admin.isTableAvailable("newtable")==true)
    {
        System.out.println("Table created");
    }
    else
    {
        System.out.println("Table was not created");
    }
    
    
    }
       
}

No comments:

Post a Comment