HBase Manager design example

HBase Manager design example

HBase Manager design example

This post help you to design your hbase manager class which can have common hbase utility methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
public class HBaseManager
{
  private static Logger logger = Logger.getLogger( HBaseManager.class );
  private HBaseAdmin admin;
  private HTablePool pool;
  private Configuration conf;
 
  public HBaseManager( String zkQuorumHost, String zkQuorumPort ) {
    super();
    logger.info( "Hbase is intializing" );
    try {
      logger.info( "HBase Manager Constructor begin zkQuorumHost = " + zkQuorumHost );
      conf = HBaseConfiguration.create();
      conf.set( "zk.quorum.host", zkQuorumHost );
      conf.set( "zk.quorum.port", zkQuorumPort );
      admin = new HBaseAdmin( conf );
      pool = new HTablePool( conf, 30 );
    } catch( MasterNotRunningException e ) {
       logger.error("Master is Not Running " ,e );
    } catch( ZooKeeperConnectionException e ) {
      logger.error("Zookeeper connection failed" ,e );
    }
  }
 
  public HTablePool getTablePool()
  {
    return pool;
  }
 
  public boolean closeTablePool( String tableName )
    throws IOException
  {
    pool.closeTablePool( tableName );
    return true;
  }
 
  public boolean createTable( String tableName, String ... columnFamilies )
    throws IOException
  {
    logger.info( "Createing table" );
    HTableDescriptor table = null;
    HColumnDescriptor columnFamily;
    int maxversion = 1;
    table = new HTableDescriptor( tableName );
    for( String cf : columnFamilies ) {
      columnFamily = new HColumnDescriptor( cf );
      columnFamily.setMaxVersions( maxversion );
      table.addFamily( columnFamily );
    }
    admin.createTable( table );
    return true;
  }
 public static boolean checkIfRunning( Configuration conf )
    throws MasterNotRunningException, ZooKeeperConnectionException
  {
    HBaseAdmin.checkHBaseAvailable( conf );
    return true;
  }
 
  public boolean checkIfRunning()
    throws MasterNotRunningException, ZooKeeperConnectionException
  {
    HBaseAdmin.checkHBaseAvailable( conf );
    return true;
  }
 
  /**
   * Flush a table. Asynchronous operation
   * @param table
   *          to be flushed
   * @throws IOException
   */
  public void flush( String tableName )
    throws IOException, InterruptedException
  {
    admin.flush( tableName );
  }
 
  /**
   * @param
   * @return <tt>true </tt>if table exists
   * @throws IOException
   */
  public boolean isTableExists( String tableName )
    throws IOException
  {
    return admin.tableExists( tableName );
  }
 
  /**
   * @param tablename
   * @return true if table available
   * @throws IOException
   */
  public boolean isTableAvailable( String tableName )
    throws IOException
  {
    return admin.isTableAvailable( tableName.getBytes() );
 
  }
 
  /**
   * @return array of available tables
   * @throws IOException
   */
  public HTableDescriptor[] listTables()
    throws IOException
  {
    return admin.listTables();
  }

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.