gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:35:57 24 4
gpt4 key购买 nike

在我们的环境中,我们使用多线程通过hbase Java Client调用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 等。它们打开的资源必须在您完成处理后关闭。

关于java - 多线程之间能否复用hbase Java client Connection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41499087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com