简体   繁体   English

Tomcat中的HBase连接池?

[英]HBase connection pool in Tomcat?

I have a small HBase cluster running with a Java frontend. 我有一个运行Java前端的小型HBase集群。 I have it working successfully with test code that establishes a connection, runs test data, and disconnects, but I need to extend it into a webapp as part of a larger project. 我已将其与建立连接,运行测试数据并断开连接的测试代码一起成功使用,但是作为一个较大项目的一部分,我需要将其扩展到一个webapp中。 It was suggested that I use Tomcat as the HTTP service to interface with the database, and as part of this I need to set up a connection pool. 有人建议我将Tomcat用作HTTP服务以与数据库进行接口连接,并且在此过程中,我需要设置连接池。

I understand connection pools on a conceptual level (a pile of persistent connections to the DB handed out to clients as needed and returned to the pool when discarded), but I've never written a webapp before and Tomcat is vexxing me. 我了解概念上的连接池(一堆与数据库的持久连接会根据需要分发给客户端,并在丢弃时返回到池中),但是我之前从未编写过Webapp,而Tomcat正在困扰我。 I can find loads of documentation on how to setup a connection pool to a SQL server using JDBC , but nothing on HBase (or, for that matter, anything BUT SQL ) and nothing on how to (or whether I can or should) write a custom interface to get it working with Tomcat . 我可以找到大量有关如何使用JDBC设置到SQL Server的连接池的文档,但是在HBase什么也没有(或者,什么都可以,但是BUT SQL ),如何(或者是否可以或应该)写定制接口使其与Tomcat一起使用。 Is this doable, or even possible, or should I be taking a different approach? 这是可行的,甚至是可能的,还是我应该采用其他方法?

I think it would be hard to find something that is made for Tomcat , I don't think that such thing exist. 我认为很难找到适用于Tomcat ,但我认为这并不存在。 Most people are probably using custom classes. 大多数人可能正在使用自定义类。 That being said, you should probably build something around the HTablePool that comes with HBase . 话虽如此,您可能应该围绕HBase附带的HTablePool建立一些东西。

Here an example of something you can use in your project : 这是您可以在项目中使用的示例:

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

And then you can use it like this : 然后您可以像这样使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM