This topic describes how to use the HBase Java API to access LindormTable and provides examples.
Prerequisites
Java Development Kit (JDK) V1.8 or later is installed.
You have obtained the endpoint for HBase Java API access.
You have added the client IP address to the Lindorm whitelist.
You have installed the HBase Java SDK.
Procedure
Configure the client to connect to a Lindorm instance in one of the following ways.
Add the following configuration items to the
hbase-site.xmlconfiguration file.<configuration> <!-- The endpoint of the cluster. Obtain it from the Database Connection page in the console. Note the difference between public and VPC endpoints. --> <property> <name>hbase.zookeeper.quorum</name> <value>ld-xxxx-proxy-hbaseue.lindormue.xxx.rds.aliyuncs.com:30020</value> </property> <!-- Set the username and password. The default for both is root. Modify them as needed. --> <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 new Configuration object. Configuration conf = HBaseConfiguration.create(); // The endpoint of the instance. You can obtain it from the Database Connection page in the 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");Parameters
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 with only one partition. // A single region limits the cluster's parallel processing and load balancing capabilities. Therefore, pre-partition the table based on your data characteristics when you create it. If all data is stored in a single partition, parallel processing and load balancing across multiple partitions cannot be achieved. This can lead to performance bottlenecks and data hot spots. admin.createTable(htd); // To create a table with multiple partitions: // The following example shows how to pre-partition a table into two partitions: [-∞, 10) and [10, ∞). Generate partition information as needed. // int numRegions = 2; // Set the number of regions to create. // 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 the table. admin.disableTable(TableName.valueOf("tablename")); // Truncate the table to delete all data. You must disable the table before truncating it. admin.truncateTable(TableName.valueOf("tablename"), true); // Delete the table. You must disable the table before deleting it. admin.deleteTable(TableName.valueOf("tablename")); }DML operations
// The Table object is not thread-safe. Each thread must get its own Table object from the Connection when performing table operations. 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. Get get = new Get(Bytes.toBytes("row")); Result res = table.get(get); // Delete a row. Delete delete = new Delete(Bytes.toBytes("row")); table.delete(delete); // Scan a range of data. Scan scan = new Scan(Bytes.toBytes("startRow"), Bytes.toBytes("endRow")); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { // Process the query result. // ... } scanner.close(); }