Updated on 2024-04-02 GMT+08:00

Creating Connection

Function

HBase creates a Connection object using the ConnectionFactory.createConnection(configuration) method. The transferred parameter is the Configuration created in the last step.

Connection encapsulates the connections between underlying applications and servers and ZooKeeper. Connection is instantiated using the ConnectionFactory class. Creating Connection is a heavyweight operation. Connection is thread-safe. Therefore, multiple client threads can share one Connection.

In a typical scenario, a client program uses a Connection, and each thread obtains its own Admin or Table instance and invokes the operation interface provided by the Admin or Table object. You are not advised to cache or pool Table and Admin. The lifecycle of Connection is maintained by invokers that frees up resources by invoking close().

Example Code

The following code snippet exemplifies login, creating Connection, and creating a table. It belongs to the HbaseSample method in the HbaseSample class of the com.huawei.bigdata.hbase.examples package.

    private TableName tableName = null;
    private Connection conn = null;
 
    public HBaseSample(Configuration conf) throws IOException {
        this.tableName = TableName.valueOf("hbase_sample_table");
        this.conn = ConnectionFactory.createConnection(conf);
}

Avoid invoking login code repeatedly.