获得hbase的htable句柄的最佳方法是什么?

vsikbqxv  于 2021-06-07  发布在  Hbase
关注(0)|答案(1)|浏览(452)

一种方法是直接调用htable构造函数,另一种方法是从hconnection调用gettable方法。第二个选项要求hconnection为“非托管”,这对我来说不是很好,因为我的进程将有许多线程访问hbase。我不想重新发明轮子来管理我自己的连接。
谢谢你的帮助。
[更新]:我们使用的是0.98.6,因此connectionfactory不可用。
我发现下面的jira建议创建一个“非托管”连接,并使用单个executeservice创建htable。为什么我们不能简单地使用非托管连接的gettable方法来获取htable呢?这是因为htable不是线程安全的吗?https://issues.apache.org/jira/browse/hbase-7463

pokxtpni

pokxtpni1#

我坚持使用旧版本(<0.94.11),你仍然可以使用 HTablePool 但是由于hbase-6580已经不赞成使用它,我认为从htables到rs的请求现在通过提供 ExecutorService :

  1. ExecutorService executor = Executors.newFixedThreadPool(10);
  2. Connection connection = ConnectionFactory.createConnection(conf, executor);
  3. Table table = connection.getTable(TableName.valueOf("mytable"));
  4. try {
  5. table.get(...);
  6. ...
  7. } finally {
  8. table.close();
  9. connection.close();
  10. }

我一直找不到任何关于它的好例子/文档,所以请注意这是未经测试的代码,可能无法按预期工作。
有关更多信息,请参阅connectionfactory文档和jira问题:https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/connectionfactory.htmlhttpshttp://issues.apache.org/jira/browse/hbase-6580
更新,因为您使用的是0.98.6并且connectionfactory不可用,所以可以改用hconnectionmanager:

  1. HConnection connection = HConnectionManager.createConnection(config); // You can also provide an ExecutorService if you want to override the default one. HConnection is thread safe.
  2. HTableInterface table = connection.getTable("table1");
  3. try {
  4. // Use the table as needed, for a single operation and a single thread
  5. } finally {
  6. table.close();
  7. connection.close();
  8. }

htable不是线程安全的,所以您必须确保总是使用 HTableInterface table = connection.getTable("table1") 然后用 table.close() .
流程为:
开始你的过程
初始化hconnection
每条线:
3.1从您的hconnection获取表
3.2从表中写入/读取
3.3关闭表格
进程结束时关闭hconnection
H连接管理器:http://archive.cloudera.com/cdh5/cdh/5/hbase/apidocs/org/apache/hadoop/hbase/client/hconnectionmanager.html#createconnection(org.apache.hadoop.conf.configuration)
表:http://archive.cloudera.com/cdh5/cdh/5/hbase/apidocs/org/apache/hadoop/hbase/client/htable.html

展开查看全部

相关问题