获取 HBase 的 HTable 句柄的最佳方法是什么?
What's the best way to get HTable handle of HBase?
一种是直接调用HTable的构造函数,另一种是从HConnection中调用getTable方法。第二个选项要求 HConnection 为 "unmanaged",这对我来说不是很好,因为我的进程将有很多线程访问 HBase。我不想自己重新发明轮子来管理 HConnections。
感谢您的帮助。
[更新]:
我们坚持使用 0.98.6,因此 ConnectionFactory 不可用。
我发现下面的 jira 建议创建一个 "unmanaged" 连接并使用单个 ExecuteService 创建 HTable。为什么我们不能简单地使用非托管连接的getTable方法来获取HTable呢?那是因为 HTable 不是线程安全的吗?
https://issues.apache.org/jira/browse/HBASE-7463
我坚持使用旧版本 (<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();
}
I've been unable to find any good examples/docs about it, so please notice this is untested code which may not work as expected.
有关更多信息,您可以查看 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()
.
关闭它
流程为:
- 开始你的过程
- 初始化您的 HConnection
- 每个线程:
- 3.1 从您的 HConnection
获取 table
- 3.2 Writes/reads 来自 table
- 3.3 关闭 table
- 进程结束时关闭 HConnection
HConnectionManager:http://archive.cloudera.com/cdh5/cdh/5/hbase/apidocs/org/apache/hadoop/hbase/client/HConnectionManager.html#createConnection(org.apache.hadoop.conf.Configuration)
H表:http://archive.cloudera.com/cdh5/cdh/5/hbase/apidocs/org/apache/hadoop/hbase/client/HTable.html
一种是直接调用HTable的构造函数,另一种是从HConnection中调用getTable方法。第二个选项要求 HConnection 为 "unmanaged",这对我来说不是很好,因为我的进程将有很多线程访问 HBase。我不想自己重新发明轮子来管理 HConnections。
感谢您的帮助。
[更新]: 我们坚持使用 0.98.6,因此 ConnectionFactory 不可用。
我发现下面的 jira 建议创建一个 "unmanaged" 连接并使用单个 ExecuteService 创建 HTable。为什么我们不能简单地使用非托管连接的getTable方法来获取HTable呢?那是因为 HTable 不是线程安全的吗? https://issues.apache.org/jira/browse/HBASE-7463
我坚持使用旧版本 (<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();
}
I've been unable to find any good examples/docs about it, so please notice this is untested code which may not work as expected.
有关更多信息,您可以查看 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()
.
流程为:
- 开始你的过程
- 初始化您的 HConnection
- 每个线程:
- 3.1 从您的 HConnection 获取 table
- 3.2 Writes/reads 来自 table
- 3.3 关闭 table
- 进程结束时关闭 HConnection
HConnectionManager:http://archive.cloudera.com/cdh5/cdh/5/hbase/apidocs/org/apache/hadoop/hbase/client/HConnectionManager.html#createConnection(org.apache.hadoop.conf.Configuration)
H表:http://archive.cloudera.com/cdh5/cdh/5/hbase/apidocs/org/apache/hadoop/hbase/client/HTable.html