HBase HTablePool: correct usage

扶醉桌前 提交于 2019-12-07 07:58:18

问题


What is the correct usage pattern of HTablePool? I mean, assume that I have my DAO which is initialised with an instance of HTablePool. This DAO is a member instance of a Stateless Session Bean so it is reused between invocations.

What is the correct usage beween the following?

private HTableInterface aTable;

public XYZDAO(final HTablePool pool)
{
    this.aTable = pool.getTable(...);
}

public void doSomething(...)
{
    aTable.get(...)
}

or HTablePool should be used like a Datasource and therefore is more appropriate a usage like this

private HTablePool datasource;

public XYZDAO(final HTablePool pool)
{
    this.datasource = pool;
}

public void doSomething(...)
{
    HTableInterface aTable = datasource.getTable(...);
    aTable.get(...);
    aTable.close();
}

回答1:


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

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




回答2:


Yes the second approach is better but rather than closing the Table you should put it back in 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);
}

Example Code taken from the Book "HBase: The Definitive Guide".

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

This method is not needed anymore, clients should call HTableInterface.close() rather than returning the tables to the pool Once you are done with it, close your instance of HTableInterface by calling HTableInterface.close() rather than returning the tables to the pool with (deprecated) putTable(HTableInterface).



来源:https://stackoverflow.com/questions/12190826/hbase-htablepool-correct-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!