HBase HTablePool: Proper Use

What is the correct model for using HTablePool? I mean, suppose I have my DAO that is initialized with an instance of HTablePool. This DAO is an instance of a non-Bean session member, so it is reused between calls.

What is the correct use associated with the following?

private HTableInterface aTable; public XYZDAO(final HTablePool pool) { this.aTable = pool.getTable(...); } public void doSomething(...) { aTable.get(...) } 

or HTablePool should be used as a data source and therefore it is more appropriate to use this as

 private HTablePool datasource; public XYZDAO(final HTablePool pool) { this.datasource = pool; } public void doSomething(...) { HTableInterface aTable = datasource.getTable(...); aTable.get(...); aTable.close(); } 
+4
source share
2 answers

The second approach is the best, you should use an HTablePool , as it was a Datasource , since the HTable class HTable not thread safe. Calling the close method HTableInterface will automatically return the table to the pool.

Note that there is a HConnection interface that replaces the deprecated HTablePool in newer versions of HBase.

+4
source

Yes, the second approach is better but instead of closing the table you should return it to the pool :

 public void createUser(String username, String firstName, String lastName, String email, String password, String roles) throws IOException { HTable table = rm.getTable(UserTable.NAME); Put put = new Put(Bytes.toBytes(username)); put.add(UserTable.DATA_FAMILY, UserTable.FIRSTNAME, Bytes.toBytes(firstName)); put.add(UserTable.DATA_FAMILY, UserTable.LASTNAME, Bytes.toBytes(lastName)); put.add(UserTable.DATA_FAMILY, UserTable.EMAIL, Bytes.toBytes(email)); put.add(UserTable.DATA_FAMILY, UserTable.CREDENTIALS, Bytes.toBytes(password)); put.add(UserTable.DATA_FAMILY, UserTable.ROLES, Bytes.toBytes(roles)); table.put(put); table.flushCommits(); rm.putTable(table); } 

Sample code from HBase: The Ultimate Guide.

EDIT: I'm wrong doc after v0.92:

This method is no longer needed, clients should call HTableInterface.close (), and not return tables to the pool. When finished, close your HTableInterface instance by calling HTableInterface.close () rather than returning the tables to the pool with (deprecated) putTable (HTableInterface).

+1
source

All Articles