gpt4 book ai didi

hbase - 在java中处理hbase连接和表的最佳实践?

转载 作者:行者123 更新时间:2023-12-02 11:21:50 24 4
gpt4 key购买 nike

我正在使用 hbase-client 1.2.3,我看到 Connection.getTable() 方法中有一条评论:

  • Retrieve a Table implementation for accessing a table.

  • The returned Table is not thread safe, a new instance should be created for each using thread.

  • This is a lightweight operation, pooling or caching of the returned Table

  • is neither required nor desired.



所以我开始想知道处理连接和表的最佳实践是什么?

例如,我有一个主类,将启动几个线程,让我们调用 A、B、C...

现在我调用“Connection connection = ConnectionFactory.createConnection();”在 main 方法中,并将连接作为参数传递给每个线程。然后在每个线程中初始化 Table 类。

我想知道这是最好的方法吗?它会导致一些线程安全或效率或任何其他问题吗?

最佳答案

根据documentation :

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.



所以我建议你只初始化一次连接,每次需要时都初始化一个新表。

示例代码:

//initialize connection
Configuration hBaseConfig = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(hBaseConfig);

//initialize table
try (Table table = connection.getTable(TableName.valueOf("table-name"))) {
//use table
}

关于hbase - 在java中处理hbase连接和表的最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43600559/

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