【问题标题】:HBase connection pool in Tomcat?Tomcat中的HBase连接池?
【发布时间】:2013-07-15 18:26:55
【问题描述】:

我有一个小型 HBase 集群,使用 Java 前端运行。我让它与建立连接、运行测试数据和断开连接的测试代码一起成功地工作,但我需要将它扩展到一个 webapp 作为更大项目的一部分。有人建议我使用Tomcat 作为HTTP 服务来连接数据库,作为其中的一部分,我需要设置一个连接池。

我在概念层面上理解连接池(一堆到数据库的持久连接根据需要分发给客户端并在丢弃时返回到池中),但我之前从未编写过 webapp,Tomcat 令人烦恼我。我可以找到大量关于如何使用 JDBC 设置与 SQL 服务器的连接池的文档,但在 HBase 上没有任何内容(或者,就此而言,除了 SQL 之外的任何内容),也没有关于如何(或我是否可以或应该)编写一个自定义界面以使其与Tomcat一起工作。这是可行的,甚至是可能的,还是我应该采取不同的方法?

【问题讨论】:

    标签: java web-services tomcat hbase


    【解决方案1】:

    我认为很难找到为Tomcat 制作的东西,我认为这种东西不存在。大多数人可能正在使用自定义类。话虽如此,您可能应该围绕HBase 附带的HTablePool 构建一些东西。

    这是您可以在项目中使用的示例:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 1970-01-01
    • 2013-05-24
    • 2012-07-12
    • 1970-01-01
    相关资源
    最近更新 更多