HBase - 正确的Put方法

最近在做Hbase的一些操作,目的是利用Hbase建立一个支持高效地理索引的空间数据库,在实践中发现了一些问题,特地来这边记录下

Put的方法:

本人的实验场景是Hbase 1.2.1 + Hadoop 2.6.3,算是比较新的版本了。在最初学习Hbase是利用《Hbase实战》这本小人书,但是书上所提到的方法是利用HTable或者HTablePool等已经被弃用的方法,在这里依照1.2.1的版本总结下我目前学到的几种Put方法。

1.Table式的方法:

这是唯一没有被弃用的方法,大家看到这里如果不想深入研究下去直接用这个就好了,至少在1.2.1下没有被弃用的迹象:

public boolean put(Put put, Configuration cfg, Connection conn, TableName tableName) {
    try {
        Table table = conn.getTable(tableName);
        table.put(put);
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

以上是段简单的示例代码,流程就是根据Connection类以及TableName类生成Table对象,并利用Table对象进行表格操作。

在这段时间Hbase API的代码阅读中可以发现String和Bytes类型的TableName当做参数的函数基本都被弃用了,所以使用TableName这个类才是正道。

不过TableName生成的方法目前貌似只有valueOf这个,不知道以后会不会有其他的构建器。

按照官方文档所说,Connection对象的初始化相对耗费资源,所以在Put的过程中应该尽量不新建Connection对象。

2.HTablePool式:

private static Configuration CFG = HBaseConfiguration.create();
private static HTablePool hTablePool = new HTablePool(CFG,100);
public boolean put(Put put) {
    HTableInterface userTable = hTablePool.getTable("user");
    try {
        userTable.put(put);
        userTable.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

HTable是HBase的client,负责从meta表中找到目标数据所在的RegionServers,当定位到目标RegionServers后,client直接和RegionServers交互,而不比再经过master。

而HTablePool就是新建的HTable线程池,但是被弃用了,《HBase实战》就是使用的这个方法去进行Put操作

构建HTableInterface实现是非常轻量级的,并且资源是可控的。

没有其他了

以下来自大神甘道夫
http://blog.csdn.net/u010967382/article/details/38046821

我查看了Git上最新版本的代码(https://git-wip-us.apache.org/repos/asf?p=hbase.git;a=tree),发现getConnection复活了:

 /**
   * Get the connection that goes with the passed conf 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 conf
   * @throws ZooKeeperConnectionException
   */
  public static HConnection getConnection(final Configuration conf) throws IOException {
      return ConnectionManager.getConnectionInternal(conf);
  }  

这个不是重点,重点是最新版本代码的pom:

39   org.apache.hbase

40   hbase

41   pom

42   <version>2.0.0-SNAPSHOTversion>

43   <name>HBasename>

44   

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   

HBase即将迎来2.0.0版本!!
HBase的下一个发布版是否会像Hadoop2.0那样来一个华丽丽的升华,迎来众多牛逼的新特性呢?
从CHANGES.txt文档中没法得到最新的信息,最后一次更新还在2012年2月24日,看来开源大佬们也是爱编码不爱写文档的主。。

你可能感兴趣的:(Hbase)