获得HBase的HTable句柄的最佳方法是什么?

Adr*_*Liu 4 apache hbase

一种方法是直接调用HTable构造函数,另一种方法是从HConnection调用getTable方法.第二个选项要求HConnection为"非托管",这对我来说不是很好,因为我的进程将有许多线程访问HBase.我不想重新发明轮子来自己管理HConnections.

谢谢你的帮助.

[更新]:我们坚持使用0.98.6,因此ConnectionFactory不可用.

我发现bellow jira建议创建一个"非托管"连接并使用单个ExecuteService来创建HTable.为什么我们不能简单地使用非托管连接的getTable方法来获取HTable?那是因为HTable不是线程安全吗? https://issues.apache.org/jira/browse/HBASE-7463

Rub*_*eda 6

我坚持使用旧版本(<0.94.11),你仍然可以使用HTablePool它,但由于它已被HBASE-6580弃用,我认为从HTables到RS的请求现在通过提供以下内容自动合并ExecutorService:

ExecutorService executor = Executors.newFixedThreadPool(10);
Connection connection = ConnectionFactory.createConnection(conf, executor);
Table table = connection.getTable(TableName.valueOf("mytable"));
try {
    table.get(...);
    ...
} finally {
    table.close();
    connection.close();
}
Run Code Online (Sandbox Code Playgroud)

我一直无法找到任何关于它的好示例/文档,因此请注意这是未经测试的代码,可能无法正常工作.

欲了解更多信息,你可以看看到的ConnectionFactory文档和到JIRA问题: https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/ConnectionFactory.html HTTPS://issues.apache .ORG/JIRA /浏览/ HBASE-6580

更新,因为您使用的是0.98.6且ConnectionFactory不可用,您可以使用HConnectionManager:

HConnection connection = HConnectionManager.createConnection(config); // You can also provide an ExecutorService if you want to override the default one. HConnection is thread safe.
HTableInterface table = connection.getTable("table1");
try {
  // Use the table as needed, for a single operation and a single thread
} finally {
  table.close();
  connection.close();
}
Run Code Online (Sandbox Code Playgroud)

HTable不是线程安全的,所以你必须确保总是得到一个新的实例(它是一个轻量级的进程),HTableInterface table = connection.getTable("table1")然后用它关闭它table.close().

流程将是:

    1. 开始你的过程
    1. 初始化您的HConnection
    1. 每个帖子:
  • 3.1从HConnection获取一个表
  • 3.2从表中写入/读取
  • 3.3关闭表格
    1. 当您的流程结束时关闭您的HConnection

HConnectionManager:http://archive.cloudera.com/cdh5/cdh/5/hbase/apidocs/org/apache/hadoop/hbase/client/HConnectionManager.html#createConnection( org.apache.hadoop.conf.Configuration )

HTable:http://archive.cloudera.com/cdh5/cdh/5/hbase/apidocs/org/apache/hadoop/hbase/client/HTable.html