当前位置: 代码迷 >> 综合 >> Hbase1.0 客户端api---神算子
  详细解决方案

Hbase1.0 客户端api---神算子

热度:0   发布时间:2024-01-19 00:47:21.0

最近在试用Hbase1.0的客户端API,发觉变化还是挺大(以前版本也不熟)。到处都是deprecated。

现在应该是这样子:

Configuration  conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "quorum1,quorum2,quorum3");
conf.set("hbase.zookeeper.property.clientPort", "2181");


connection = ConnectionFactory.createConnection(conf);// HBase 0.99+

 //connection = HConnectionManager.createConnection(conf); // HBase <0.99


Table table = connection.getTable(TableName.valueOf("TestTable")); 


新的客户端API中,Connection处于关键位置,以下api说明翻译下:



关于 Connection接口的说明:
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.

This class replaces HConnection, which is now deprecated.

Connection封装了底层与各实际服务器的连接以及与zookeeper的连接。Connection通过  ConnectionFactory类实例化 。Connection的生命周期由调用者维护,调用者通过调用close(),释放资源。
连接对象包含发现master,定位region,缓存region位置一边当region移动后,重新矫正位置。通往各服务器的连接,meta数据缓存,zookeeper连接等,与通过Connection获取的Table,Admin实例都是共享的。
创建Connection是重量级操作。 Connection是线程安全的,因此,多个客户端线程可以共享一个Connection。Table和Admin实例,相反地,是轻量级的并且非线程安全。典型的用法,一个客户端程序共享一个单独的Connection,每一个线程获取自己的Table实例。不建议缓存或者池化(pooling)Table、Admin。



关于 ConnectionFactory.createConnection函数的说明:

public static Connection createConnection(org.apache.hadoop.conf.Configuration conf)
Create a new Connection instance using the passed  conf 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. 
The caller is responsible for calling  Connection.close() on the returned connection instance.

Connection封装了连接到集群的所有维护工作。通过返回的connection生成的table、接口等共享zookeeper连接,meta缓存,到region server以及master的连接。
调用者负责释放connection实例。

Similarly,  Connection  also returns  Admin  and  RegionLocator  implementations
类似返回table接口,Connection还可以返回Admin,RegionLoacator实例。