Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Heibaiying BigData Notes ConnectionFactory CreateConnection

From Leeroopedia


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 Table and Admin handles.

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

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment