This topic describes how to use the ApsaraDB for HBase API for Java to connect to LindormTable and provides examples.
Prerequisites
Java Development Kit (JDK) V1.8 or later is installed.
The LindormTable endpoint that is displayed after Access by Using HBase Java API on the Wide Table Engine tab of the Lindorm console is obtained. For more information, see View endpoints.
The IP address of your client is added to the whitelist of the Lindorm instance. For more information, see Configure whitelists.
ApsaraDB for HBase SDK for Java is installed. For more information, see Install ApsaraDB for HBase SDK for Java.
Procedure
Configure a client to connect to a Lindorm instance.
Add the following configuration items to the configuration file hbase-site.xml:
<configuration> <!-- // Specify the endpoint that you can use to connect to the Lindorm instance. You can view the endpoint on the Database Connections page of the Lindorm console. If you want to connect to the Lindorm instance over the Internet, use the public endpoint. If you want to connect to the Lindorm instance over a virtual private cloud (VPC), use the VPC endpoint. --> <property> <name>hbase.zookeeper.quorum</name> <value>ld-xxxx-proxy-hbaseue.lindormue.xxx.rds.aliyuncs.com:30020</value> </property> <!-- Specify the actual username and password that you use to connect to the Lindorm instance. The default username and password are both root. --> <property> <name>hbase.client.username</name> <value>testuser</value> </property> <property> <name>hbase.client.password</name> <value>password</value> </property> </configuration>
Add the following code snippet to your project to create a Configuration object and configure the parameters described in the following table.
// Create a Configuration object. Configuration conf = HBaseConfiguration.create(); // Specify the public endpoint of the Lindorm instance. You can obtain the public endpoint on the Database Connections page of the Lindorm console. conf.set("hbase.zookeeper.quorum", "host:port"); // xml_template.comment.hbaseue.username_password.default conf.set("hbase.client.username", "Username"); conf.set("hbase.client.password", "Password");
Annotation
Example
Method to obtain the parameter value
host:port
ld-bp17j28j2y7pm****-proxy-lindorm-pub.lindorm.rds.aliyuncs.com:30020
The LindormTable endpoint that is displayed after Access by Using HBase Java API on the Wide Table Engine tab of the Lindorm console. For more information, see View endpoints.
The username.
testuser
The username and password that you use to connect to the Lindorm instance. If you forget the password, you can change the password in the cluster management system of LindormTable. For more information, see Change the password of a user.
Password
password
Establish a connection between the Lindorm client and the database.
Connection connection = ConnectionFactory.createConnection(conf);
NoteThe connection is created only once during the lifecycle of the program. The connection is thread-safe and can be shared among all threads. // After the program stops running, close the Connection object to prevent a connection leak. You can also execute the try-finally statement to prevent a connection leak.
After the connection is established, use the ApsaraDB for HBase API for Java to access LindormTable. You can use the following sample Java code.
DDL operations
try (Admin admin = connection.getAdmin()){ // Create a table. HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("tablename")); htd.addFamily(new HColumnDescriptor(Bytes.toBytes("family"))); // Create a table that has only one partition. // The parallel processing and load balancing capabilities of the cluster are limited if the table has only one partition. Therefore, we recommend that you partition the table in advance based on the data characteristics. If all data is stored in the same partition, parallel processing and load balancing among partitions cannot be implemented. This may lead to performance bottlenecks and hot spot data. admin.createTable(htd); // To create a table that has multiple partitions, refer to the following sample code. // In the sample code, the table is pre-partitioned into two partitions: [-∞, 10) and [10, ∞). Specify the partition range based on your actual business. // int numRegions = 2; // Specify the number of partitions. // byte[][] splitKeys = new byte[numRegions-1][]; // for (int i = 1; i < numRegions; i++) { // splitKeys[i-1] = new byte[]{(byte)(i * 10)}; // } // admin.createTable(tableDescriptor, splitKeys); // Disable a table. admin.disableTable(TableName.valueOf("tablename")); // Clear all data in a table. You must disable a table before you clear all data in it. admin.truncateTable(TableName.valueOf("tablename"), true); // Delete a table. You must disable a table before you delete it. admin.deleteTable(TableName.valueOf("tablename")); }
DML operations
// A table is a non-thread-safe object. When a thread performs operations on a table, the thread must obtain the required table from the Connection object. try (Table table = connection.getTable(TableName.valueOf("tablename"))) { // Insert data. Put put = new Put(Bytes.toBytes("row")); put.addColumn(Bytes.toBytes("family"), Bytes.toBytes("qualifier"), Bytes.toBytes("value")); table.put(put); // Read a single row of data. Get get = new Get(Bytes.toBytes("row")); Result res = table.get(get); // Delete a row of data. Delete delete = new Delete(Bytes.toBytes("row")); table.delete(delete); // Scan the data in the specified range. Scan scan = new Scan(Bytes.toBytes("startRow"), Bytes.toBytes("endRow")); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { // Process the result. // ... } scanner.close(); }