【问题标题】:What's the best way to get HTable handle of HBase?获得 HBase 的 HTable 句柄的最佳方法是什么?
【发布时间】:2015-04-07 02:38:27
【问题描述】:

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

感谢您的帮助。

[更新]: 我们被 0.98.6 卡住了,所以 ConnectionFactory 不可用。

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

【问题讨论】:

    标签: apache hbase


    【解决方案1】:

    我坚持使用旧版本 (HTablePool,但由于它已被 HBASE-6580 弃用,我认为从 HTables 到 RS 的请求现在通过提供 @987654327 自动汇集@:

    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();
    }
    

    我找不到任何好的示例/文档,所以请注意这是未经测试的代码,可能无法按预期工作。

    有关更多信息,您可以查看 ConnectionFactory 文档和 JIRA 问题: https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/ConnectionFactory.html https://issues.apache.org/jira/browse/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();
    }
    

    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

    【讨论】:

    • 感谢您的回复。忘了提到我们坚持使用 0.98.6,所以没有 ConnectionFactory。我更新了我的问题并添加了另一个选项供您选择。
    • 我已经更新了我的答案,希望现在更清楚:)
    猜你喜欢
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 2013-08-25
    • 2016-05-21
    相关资源
    最近更新 更多