Example 9.1. Pre-Creating a HConnection
// Create a connection to the cluster. HConnection connection = HConnectionManager.createConnection(Configuration); HTableInterface table = connection.getTable("myTable"); // use table as needed, the table returned is lightweight table.close(); // use the connection for other access to the cluster connection.close(); 构建HTableInterface实现是很轻量级的,而且资源是可控的。 注意: HTablePool是HBase连接池的老使用方法。该类在0.94,0.95和0.96中已经不建议使用。在0.98.1版本号以后已经移除。通过这个HConnection实例,能够使用
HConnection.getTable(byte[])方法取得 HTableInterface implementations的实现,比如 : HConnection connection = HConnectionManager . createConnection ( config ); HTableInterface table = connection.getTable("tablename"); try { // Use the table as needed, for a single operation and a single thread } finally { table.close(); connection.close(); } 3.1构造函数 无。不可实例化。 3.2经常用法 (1)static HConnection createConnection(org.apache.hadoop.conf.Configuration conf) 创建一个新的HConnection实例。 该方法绕过了常规的HConnection生命周期管理,常规是通过 getConnection(Configuration)来获取连接。调用方负责运行
Closeable.close() 来关闭获得的连接实例。 推荐的创建HConnection的方法是: HConnection connection = HConnectionManager.createConnection(conf); HTableInterface table = connection.getTable("mytable"); table.get(...); ... table.close(); connection.close(); (2)public static HConnection getConnection(org.apache.hadoop.conf.Configuration conf) 依据conf获取连接实例。假设没有相应的连接实例存在,该方法创建一个新的连接。
注意:该方法在0.96和0.98版本号中都被Deprecated了,不建议使用。可是在最新的未公布代码版本号中又复活了。!!
3.3实例代码 package fulong.bigdata.hbase; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HConnection; import org.apache.hadoop.hbase.client.HConnectionManager; import org.apache.hadoop.hbase.client.HTableInterface; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; public class ConnectionPoolTest { private static final String QUORUM = "FBI001,FBI002,FBI003"; private static final String CLIENTPORT = "2181"; private static final String TABLENAME = "rd_ns:itable"; private static Configuration conf = null; private static HConnection conn = null; static{ try { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", QUORUM); conf.set("hbase.zookeeper.property.clientPort", CLIENTPORT); conn = HConnectionManager.createConnection(conf); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { HTableInterface htable = ConnectionPoolTest.conn.getTable(TABLENAME); try { Scan scan = new Scan(); ResultScanner rs = htable.getScanner(scan); for (Result r : rs.next(5)) { for (Cell cell : r.rawCells()) { System.out.println("Rowkey : " + Bytes.toString(r.getRow()) + " Familiy:Quilifier : " + Bytes.toString(CellUtil.cloneQualifier(cell)) + " Value : " + Bytes.toString(CellUtil.cloneValue(cell)) + " Time : " + cell.getTimestamp()); } } } finally { htable.close(); } } }那“池”的意义就不大了。
所以,代码中才将基于 getConnection 获取池中物的机制 Deprecated了,转而在官方文档中建议: ******************************************************************************************************************* 当面对多线程訪问需求时,我们能够预先建立HConnection,參见下面代码:Example 9.1. Pre-Creating a HConnection
// Create a connection to the cluster. HConnection connection = HConnectionManager.createConnection(Configuration); HTableInterface table = connection.getTable("myTable"); // use table as needed, the table returned is lightweight table.close(); // use the connection for other access to the cluster connection.close(); 构建HTableInterface实现是很轻量级的,而且资源是可控的。 ******************************************************************************************************************* (以上又一次拷贝了一次官方文档的翻译) 假设大家依照官方文档的建议做了,也就是预先创建了一个连接,以后的訪问都共享该连接,这种效果事实上和过去的 getConnection 全然一样。都是在玩一个connection实例!p=hbase.git;a=tree),发现getConnection复活了:
/** * Get the connection that goes with the passed <code>conf</code> configuration instance. * If no current connection exists, method creates a new connection and keys it using * connection-specific properties from the passed {@link Configuration}; see * {@link HConnectionKey}. * @param conf configuration * @return HConnection object for <code>conf</code> * @throws ZooKeeperConnectionException */ public static HConnection getConnection(final Configuration conf) throws IOException { return ConnectionManager.getConnectionInternal(conf); } 这个不是重点,重点是最新版本号代码的pom: 39 <groupId>org.apache.hbase</groupId> 40 <artifactId>hbase</artifactId> 41 <packaging>pom</packaging> 42 <version> 2.0.0-SNAPSHOT</version> 43 <name>HBase</name> 44 <description> 45 Apache HBase \99 is the Hadoop database. Use it when you need 46 random, realtime read/write access to your Big Data. 47 This project's goal is the hosting of very large tables -- billions of rows X millions of columns -- atop clusters 48 of commodity hardware. 49 </description> HBase即将迎来2.0.0版本号!! HBase的下一个公布版是否会像Hadoop2.0那样来一个华丽丽的升华。迎来众多牛逼的新特性呢? 从 CHANGES.txt 文档中没法得到最新的信息。最后一次更新还在2012年2月24日,看来开源大佬们也是爱编码不爱写文档的主。。
版权声明:本文博主原创文章,博客,未经同意不得转载。
转载于:https://www.cnblogs.com/bhlsheji/p/4889493.html
相关资源:数据结构—成绩单生成器