How to Create A Table in HBase

Classes used:-

Configuration -to set configuration for hbase like ip address,port,time to wait for establishing a connection
HBaseAdmin – has a createTable(table) method
HTableDescriptor- represents a Hbase Table
HColumnDescriptor-represents a Col. family in a Hbase table

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

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.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.io.hfile.Compression.Algorithm;
import org.apache.hadoop.hbase.util.Bytes;

public class CreateInsert {

public static void main(String[] args) {
String name=”SubscriberDemo”;
try {
createTable(name);
} catch (IOException e) {

e.printStackTrace();
}
}
public static void createTable(String name) throws IOException{
Configuration conf = HBaseConfiguration.create();
conf.setInt(“timeout”, 120000);
conf.set(“hbase.master”,”localhost:60000″);
conf.set(“hbase.zookeeper.quorum”,”localhost”);
conf.set(“hbase.zookeeper.property.clientPort”, “2181”);
HBaseAdmin admin = new HBaseAdmin(conf);
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
byte[] tableName = Bytes.toBytes(name);
HTableDescriptor table = new HTableDescriptor(tableName);
HColumnDescriptor family = new HColumnDescriptor(Bytes.toBytes(“virtualidinfo”));
family.setCompressionType(Algorithm.GZ);
table.addFamily(family);

System.out.println(“Table “+name+” exist: “+admin.tableExists(tableName)) ;
System.out.println(“Creating “+name+” table…”);
admin.createTable(table);
System.out.println(“Table “+name+” exist: “+
admin.tableExists(tableName)) ;
admin.close();
System.out.println(“Done……”);
}
}