Posted onEdited onInHadoop
,
HBaseViews: Valine: Symbols count in article: 5.5kReading time ≈5 mins.
HBase 连接
以 alihbase-client 2.8.6 jar 为例
1. 连接的定义
1 2 3 4 5
A cluster connection encapsulating lower level individual connections to actual servers and a connection to zookeeper. Connections are instantiated through the ConnectionFactory class. The lifecycle of the connection is managed by the caller, who has to close() the connection to release the resources.
The connection object contains logic to find the master, locate regions out on the cluster, keeps a cache of locations and then knows how to re-calibrate after they move. The individual connections to servers, meta cache, zookeeper connection, etc are all shared by the Table and Admin instances obtained from this connection.
Connection creation is a heavy-weight operation. Connection implementations are thread-safe, so that the client can create a connection once, and share it with different threads. Table and Admin instances, on the other hand, are light-weight and are not thread-safe. Typically, a single connection per client application is instantiated and every thread will obtain its own Table instance. Caching or pooling of Table and Admin is not recommended.
/** * Create a new Connection instance using the passed <code>conf</code> instance. Connection * encapsulates all housekeeping for a connection to the cluster. All tables and interfaces * created from returned connection share zookeeper connection, meta cache, and connections to * region servers and masters. <br> * The caller is responsible for calling {@link Connection#close()} on the returned connection * instance. Typical usage: * * Connection connection = ConnectionFactory.createConnection(conf); * Table table = connection.getTable(TableName.valueOf("table1")); * try { * table.get(...); * ... * } finally { * table.close(); * connection.close(); * } */ publicstatic Connection createConnection(Configuration conf, ExecutorService pool, User user) throws IOException { setupHBaseUEParamsIfNeeded(conf); setupHBaseMultiParamsIfNeeded(conf); if (user == null) { UserProviderprovider= UserProvider.instantiate(conf); user = provider.getCurrent(); }
publicConnectionId(User ticket, String serviceName, InetSocketAddress address) { this.address = address; this.ticket = ticket; this.serviceName = serviceName; } /** * Return the pool type specified in the configuration, which must be set to either * {@link org.apache.hadoop.hbase.util.PoolMap.PoolType#RoundRobin} or * {@link org.apache.hadoop.hbase.util.PoolMap.PoolType#ThreadLocal}, otherwise default to the * former. For applications with many user threads, use a small round-robin pool. For applications * with few user threads, you may want to try using a thread-local pool. In any case, the number * of {@link org.apache.hadoop.hbase.ipc.RpcClient} instances should not exceed the operating * system's hard limit on the number of connections. * @param config configuration * @return either a {@link org.apache.hadoop.hbase.util.PoolMap.PoolType#RoundRobin} or * {@link org.apache.hadoop.hbase.util.PoolMap.PoolType#ThreadLocal} */ privatestatic PoolMap.PoolType getPoolType(Configuration config) { return PoolMap.PoolType.valueOf(config.get(HConstants.HBASE_CLIENT_IPC_POOL_TYPE), PoolMap.PoolType.RoundRobin, PoolMap.PoolType.ThreadLocal); }