能否复用多线程之间的hbase Java client Connection?

Can we reuse the hbase Java client Connection between multi-threads?

在我们的环境中,我们使用多线程通过hbase Java客户端调用hbase,并且在每个线程中我们在完成操作时调用Connection.close(),但是我们发现Connection.close() 花费了大约 10 毫秒,所以我可以知道是否可以在不关闭连接的情况下重用线程之间的连接吗?

来自 HBase 1.2 的 API 文档,org.apache.hadoop.hbase.client.Connection:https://hbase.apache.org/1.2/apidocs/org/apache/hadoop/hbase/client/Connection.html

Connection creation is a heavy-weight operation. Connection implementations are thread-safe, so that the client can create a connection once, and share it with different threads. Table and Admin instances, on the other hand, are light-weight and are not thread-safe. Typically, a single connection per client application is instantiated and every thread will obtain its own Table instance. Caching or pooling of Table and Admin is not recommended.

This class replaces HConnection, which is now deprecated.

所以,是的,创建 HBase 连接很慢。但是获得的连接是线程安全的,您的应用程序中应该只有一个 HBase 连接(您应该只在应用程序结束/关闭时关闭)。

但是,请注意关闭您从 Connection 获得的对象:Table、Admin、ResultScanner、...它们打开的资源必须在您完成处理后关闭。