HBase连接关闭()

时间:2017-06-14 05:44:03

标签: hbase connection-pooling

在每次api通话中,我这样做:

        Configuration config = HBaseConfiguration.create();
        Path hbase = new Path("hbase-site.xml");
        Path core = new Path("core-site.xml");
        config.addResource(hbase);
        config.addResource(core);
        Connection connection = ConnectionFactory.createConnection(config);

然后在我获取当前api调用的数据后立即关闭此连接:

finally {
            if (null != connection) {
                try {
                    connection.close();
                } catch (Exception e) {

                }
            }
        }

请告诉我这个型号是对还是错。每个api请求都需要以这种方式提供 - 使用ConnectionFactory打开连接,获取数据并最终关闭它。我听说有人说连接不需要关闭。

请指教

1 个答案:

答案 0 :(得分:1)

通常,我预先创建Connection对象并将其用于多个查询。 根据{{​​3}}

In HBase 1.0, obtain a Connection object from ConnectionFactory and thereafter, get from it instances of Table, Admin, and RegionLocator on an as-need basis. When done, close the obtained instances. Finally, be sure to cleanup your Connection instance before exiting. Connections are heavyweight objects but thread-safe so you can create one for your application and keep the instance around. Table, Admin and RegionLocator instances are lightweight. Create as you go and then let go as soon as you are done by closing them.

相关问题