咱們先看HConnection的getTable方法描述:html
HTableInterface getTable(String tableName) throws IOException
Retrieve an HTableInterface implementation for access to a table. The returned HTableInterface is not thread safe, a new instance should be created for each using thread. This is a lightweight operation, pooling or caching of the returned HTableInterface is neither required nor desired. Note that the HConnection needs to be unmanaged (created withHConnectionManager.createConnection(Configuration)
).java
tableName
-
IOException
而後再聯想到只要HTable使用的Configuration是同一個,那麼它們必定是共用一個HConnection的,HConnection纔是HBase客戶端到Hbase集羣的真正的鏈接。apache
再想一想HTablePool的做用,無非就是HTable的鏈接池,裏面維護的HTable應該來講都是使用的同一個HConnecion。api
既然HTable的建立是輕量級的,使用同一個Confuguration的HTable都會共用一個HConnection,那麼HTablePool就顯得那麼多餘!緩存
因此Hbase拋棄了HTablePool,咱們惟一要作的就是保證HConnection實例是惟一的,全局共享的。而後針對HTableInterface對象最好在每次操做HBase表的時候根據HConnection對象來從新建立,使用完成以後及時關閉便可!安全
經過HConnection的getTable()方法就可以獲取到用戶操做HBase表的HTableInterface對象了。oracle
下面是一個使用HConnection的getTable()方法獲取HTableInterface對象的例子:oop
public void addUser(User user) throws IOException { HTableInterface usersTable = conn.getTable(TABLE_NAME); Put put = makePut(user); usersTable.put(put); usersTable.close(); log.info("Add a User:"+user.name+" successfully"); }
至於HConnection對象如何建立,HBase推薦使用的方法是:ui
public static HConnection createConnection(org.apache.hadoop.conf.Configuration conf) throws IOException
conf
instance.
Note: This bypasses the usual HConnection life cycle management done bygetConnection(Configuration)
. The caller is responsible for callingCloseable.close()
on the returned connection instance. This is the recommended way to create HConnections. HConnection connection = HConnectionManager.createConnection(conf); HTableInterface table = connection.getTable("mytable"); table.get(...); ... table.close(); connection.close();
this
conf
- configuration
conf
ZooKeeperConnectionException
IOException
下面代碼是我按照單例模式維護HConnection對象的例子:
public class HBaseUtils { private static final String QUORUM = "192.168.1.100"; private static final String CLIENTPORT = "2181"; private static Configuration conf = null; private static HConnection conn = null; /** * 獲取全局惟一的Configuration實例 * @return */ public static synchronized Configuration getConfiguration() { if(conf == null) { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", QUORUM); conf.set("hbase.zookeeper.property.clientPort", CLIENTPORT); } return conf; } /** * 獲取全局惟一的HConnection實例 * @return * @throws ZooKeeperConnectionException */ public static synchronized HConnection getHConnection() throws ZooKeeperConnectionException { if(conn == null) { /* * * 建立一個HConnection * HConnection connection = HConnectionManager.createConnection(conf); * HTableInterface table = connection.getTable("mytable"); * table.get(...); ... * table.close(); * connection.close(); * */ conn = HConnectionManager.createConnection(getConfiguration()); } return conn; } }
以上屬於我的看法,若是有什麼疑問和指教,歡迎指正。