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).
source share