Tomcat中的HBase连接池?
问题描述:
我有一个小型的HBase
群集,前台运行着Java
。我已经成功地使用建立连接,运行测试数据和断开连接的测试代码,但我需要将它作为更大项目的一部分扩展到Web应用程序。有人建议我使用Tomcat
作为HTTP
服务来与数据库接口,并且作为此部分,我需要建立一个连接池。Tomcat中的HBase连接池?
我理解概念级别的连接池(根据需要将一堆持久连接分发给客户端,并在丢弃时返回池),但我从未写过web应用程序,并且Tomcat
让我烦恼。我能找到的文档的负荷就如何建立一个连接池使用JDBC
一个SQL
服务器,但没有对HBase
(或者,就此而言,任何事情,但SQL
),并没有对如何(或者我是否可以或应该)写自定义界面,以使其与Tomcat
一起工作。这是可行的,甚至可能的,还是我应该采取不同的方法?
答
我认为这将很难找到为Tomcat
制作的东西,我不认为这样的东西存在。大多数人可能使用自定义类。话虽如此,你应该围绕HTablePool
建立一个HBase
。
这里的东西,你可以在你的项目中使用的例子:
public class HTableManager {
public static int HTABLE_POOL_SIZE = 15;
private static HTableManager instance;
private static HTablePool hTablePool;
/**
* Private Constructor
*/
private HTableManager() {
try {
Configuration config = HBaseConfiguration.create();
config.set("hbase.defaults.for.version.skip", "false");
hTablePool = new HTablePool(config, HTABLE_POOL_SIZE);
} catch (IOException ex) {
// Error handling
}
}
/**
* @return The HbaseTableManager instance
*/
public static HTableManager getInstance() {
if (instance == null) {
instance = new HTableManager();
}
return instance;
}
/**
* Method used to retrieve a HTable instance.
*
* @param tableName The table name
* @return The HTableInterface instance
* @throws IOException
*/
public synchronized HTableInterface getHTable(String tableName) throws IOException {
return hTablePool.getTable(tableName);
}
}
然后你就可以使用它像这样:
HTableManager hTableManager = htableManager.getInstance();
HTableInterface hTable = hTableManager.getHTable("yourTableName");
...
// Work with the hTable instance
...
// A call to the close method returns it to the pool
hTable.close();
而且,这里我cant't找到一个理由,为什么close方法reutrns表中池,请参见:http://stackoverflow.com/questions/12190826/hbase-htablepool-correct-usage/17712233#17712233 – seb
阅读的javadoc http://hbase.apache.org/apidocs /org/apache/hadoop/hbase/client/HTablePool.html –