Implementation:Heibaiying BigData Notes ConnectionFactory CreateConnection
| Knowledge Sources | |
|---|---|
| Domains | NoSQL, Big_Data |
| Last Updated | 2026-02-10 10:00 GMT |
Overview
Concrete tool for creating a thread-safe HBase cluster connection provided by org.apache.hadoop.hbase.client.ConnectionFactory.
Description
The ConnectionFactory.createConnection(Configuration) static method establishes a connection to the HBase cluster described by the given configuration. It returns a Connection object that:
- Manages the ZooKeeper session.
- Caches region locations from the hbase:meta table.
- Provides factory methods for obtaining
TableandAdminhandles.
In the BigData-Notes reference implementation, the connection is created once in a static initializer block and stored as a static field of the HBaseUtils class. This ensures that all utility methods share the same connection instance throughout the application lifecycle.
Usage
Invoke ConnectionFactory.createConnection(config) once with a fully configured Configuration object, store the result, and reuse it for all subsequent HBase operations.
Code Reference
Source Location
code/Hbase/hbase-java-api-2.x/src/main/java/com/heibaiying/HBaseUtils.java (Line 24)
Signature
// ConnectionFactory static method
public static Connection createConnection(Configuration conf) throws IOException;
Import
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.conf.Configuration;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| conf | org.apache.hadoop.conf.Configuration | Yes | A Configuration object with HBase and ZooKeeper connection properties already set (see HBaseConfiguration.create()). |
Outputs
| Name | Type | Description |
|---|---|---|
| connection | org.apache.hadoop.hbase.client.Connection | A thread-safe connection to the HBase cluster. Must be closed when no longer needed. |
Usage Examples
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
public class HBaseUtils {
private static Connection connection;
static {
try {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "hadoop001");
connection = ConnectionFactory.createConnection(configuration);
} catch (IOException e) {
e.printStackTrace();
}
}
// All utility methods use the shared 'connection' field
// ...
// Close the connection on application shutdown
public static void close() throws IOException {
if (connection != null) {
connection.close();
}
}
}
Related Pages
Implements Principle
Requires Environment
- Environment:Heibaiying_BigData_Notes_Java_8_Maven_Environment
- Environment:Heibaiying_BigData_Notes_HBase_Environment