Help Center/ GeminiDB/ GeminiDB Cassandra API/ HBase-Compatible Instance/ Connecting to a GeminiDB HBase Instance
Updated on 2024-10-08 GMT+08:00

Connecting to a GeminiDB HBase Instance

This section describes how to connect to a GeminiDB HBase instance using a private IP address and Java.

Prerequisites

Viewing the IP Address of an Instance

  1. Log in to the GeminiDB console.
  2. On the Instances page, click the name of the target instance.

    Method 1

    In the Node Information area on the Basic Information page, view the private IP address of each node in the GeminiDB HBase instance.

    Public IP addresses cannot be bound to GeminiDB HBase instances.

    Figure 1 Obtaining IP addresses

    In the Network Information area, you can view the port of the GeminiDB HBase instance. The default port displayed on the page is 8635, but the default port in use is 2181.

    Figure 2 Viewing the port number

    Method 2

    In the navigation pane on the left, click Connections.

    Figure 3 Viewing the IP addresses and port number

Connecting to an Instance over a Private Network

  1. Log in to ECS.

    For details, see Logging In to an ECS in Getting Started with Elastic Cloud Server.

  2. Upload the HBase client installation package to the ECS.
  3. Run the following command to decompress the client installation package:

    tar -xvf hbase-2.5.8-client-bin.tar.gz

  4. Run the following command to change the value of hbase.zookeeper.quorum in the conf/hbase-site.xml file to the private IP address of the instance:

    vim conf/hbase-site.xml

    The private IP address of the instance can be obtained by referring to Viewing the IP Address of an Instance.

  5. Run the following commands to go to the bin directory and run the ./hbase shell command to connect to the instance.

    cd bin
    export HADOOP_PROXY_USER=${username}
    export HADOOP_USER_NAME=${password}
    ./hbase shell

  6. If information similar to the following is displayed, the connection was successful.

    hbase:001:0>

Connecting to an Instance Using Java

  1. Obtain the private IP address and port number of the instance.

    For details about how to obtain the private IP address and port number, see Viewing the IP Address of an Instance.

  2. Log in to the ECS. For details, see "Getting Started > Logging in to an ECS" in the Elastic Cloud Server User Guide.
  3. Add the following Maven dependencies to your project.

    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-client</artifactId>
        <version>2.5.3</version>
    </dependency>

  4. Edit the code for connecting to the instance.

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    
    public class HBaseExample {
        public static void main(String[] args) throws IOException {
            // Creates a configuration object and sets HBase connection parameters.
            Configuration config = HBaseConfiguration.create();
            config.set("hbase.zookeeper.quorum", "your_hbase_instance_quorum");
            config.set("hbase.zookeeper.property.clientPort", "your_hbase_instance_port");
    
            // Enters a username and password.
            UserGroupInformation ugi = UserGroupInformation.createProxyUser("your_user_name",   UserGroupInformation.createRemoteUser("your_password"));
    
            // Establishes a connection to the HBase instance.
            Connection connection = ConnectionFactory.createConnection(config);
    
            try {
                // Obtains table objects.
                TableName tableName = TableName.valueOf("your_table_name");
                Table table = connection.getTable(tableName);
    
                // Inserts data.
                Put put = new Put(Bytes.toBytes("row_key"));
                put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col"), Bytes.toBytes("value"));
                table.put(put);
    
                // Obtains a single row of data.
                Get get = new Get(Bytes.toBytes("row_key"));
                Result result = table.get(get);
                byte[] value = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("col"));
                System.out.println("Value: " + Bytes.toString(value));
    
            } finally {
               // Closes the connection.
                connection.close();
            }
        }
    }

  5. Run the sample code to check whether the result is normal.