Help Center/
GeminiDB/
GeminiDB HBase API/
Working with GeminiDB HBase API/
Instance Connection and Management/
Connecting to a GeminiDB HBase Instance Using Program Code/
Connecting to a GeminiDB HBase Instance Using Java
Updated on 2025-08-05 GMT+08:00
Connecting to a GeminiDB HBase Instance Using Java
This section describes how to connect to a GeminiDB HBase instance using Java.
Prerequisites
- A GeminiDB HBase instance has been created and is running properly. For details about how to create a GeminiDB HBase instance, see Getting to Know GeminiDB HBase API.
- For details about how to create an ECS, see Purchasing an ECS in Getting Started with Elastic Cloud Server.
- JDK has been installed on the ECS.
- Download the HBase client. Click a directory of the latest version 2.6.X and download hbase-2.6.X-client-bin.tar.gz. For example, if the latest version is 2.6.1, click that directory and download hbase-2.6.1-client-bin.tar.gz. HBase 1.X is not recommended due to compatibility issues.
- Obtain the private IP address and port of the GeminiDB HBase instance.
For details about how to obtain the private IP address and port, see Viewing the Instance IP Address.
- Log in to the ECS. For details, see Logging In to an ECS in Getting Started with Elastic Cloud Server.
- Add the following Maven dependencies to the pom.xml file of your project. HBase 1.X is not recommended due to compatibility issues causing read and write failures. You are advised to use HBase 2.2.3 or later. SSL is supported only in HBase 2.6.0 or later.
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.6.1</version> </dependency>
- Edit the code for connecting to the GeminiDB HBase instance. Replace your_hbase_instance_quorum with the cluster IP address, your_user_name with the username (rwuser by default) you set when creating the cluster, and your_password with the password you set when creating the cluster. Before running the code, ensure that an HBase table has been created in the instance, and replace your_table_name with the table name.
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", "2181"); // 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, User.create(ugi)); 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("Success: " + Bytes.toString(value)); } finally { // Closes the connection. connection.close(); } } }
- Run the sample code to check whether the result is normal. If it is, "Success: value" is displayed.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot