Friday, 29 November 2013

Create HBase Table

Create HBase Table

package ddl;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.HBaseAdmin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Hdoop Share
 * Date: 29/11/13
 * Time: 5:54 PM
 * To change this template use File | Settings | File Templates.
 */
public class Table {
    private static Configuration configuration = HBaseConfiguration.create();
    static {
            configuration.set("hbase.zookeeper.quorum","localhost");
            configuration.set("hbase.zookeeper.property.port","2181");
    }

    public void createTable(String tableName, List<String> columnFamilyList){
        HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
        for(String columnFamily:columnFamilyList){
            HColumnDescriptor.isLegalFamilyName(columnFamily.getBytes());
            tableDescriptor.addFamily(new HColumnDescriptor(columnFamily));
        }
        try {
            HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
            hBaseAdmin.createTable(tableDescriptor);
        } catch (MasterNotRunningException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (ZooKeeperConnectionException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }


    public static void main(String[] args) {
           Table table = new Table();

           List<String> columnFamilies = new ArrayList<String>();
           columnFamilies.add("FAMILY_A");
           columnFamilies.add("FAMILY_B");
           columnFamilies.add("FAMILY_C");

           table.createTable("TABLE_NAME",columnFamilies);
    }


}




You can verify the create table using Hbase Shell :


hbase(main):002:0> describe 'TABLE_NAME'
DESCRIPTION                                                                                  ENABLED                                           
 {NAME => 'TABLE_NAME', FAMILIES => [{NAME => 'FAMILY_A', DATA_BLOCK_ENCODING => 'NONE', BLO true                                              
 OMFILTER => 'NONE', REPLICATION_SCOPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_V                                                   
 ERSIONS => '0', TTL => '2147483647', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', I                                                   
 N_MEMORY => 'false', ENCODE_ON_DISK => 'true', BLOCKCACHE => 'true'}, {NAME => 'FAMILY_B',                                                    
 DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', VERSIONS =>                                                   
  '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', KEEP_DELETED_CELLS =                                                   
 > 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK => 'true', BLOCKCACHE                                                   
  => 'true'}, {NAME => 'FAMILY_C', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REP                                                   
 LICATION_SCOPE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL =>                                                    
 '2147483647', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', EN                                                   
 CODE_ON_DISK => 'true', BLOCKCACHE => 'true'}]}                                                                                               
1 row(s) in 0.3070 seconds

No comments:

Post a Comment