LindormTable allows you to use the ApsaraDB for HBase API for Java to read and write data. This topic describes how to use the ApsaraDB for HBase API for Java to connect to LindormTable.
Prerequisites
Java Development Kit (JDK) V1.8 or later is installed.
The IP address of your client is added to the whitelist of the Lindorm instance. For more information, see Configure a whitelist.
ApsaraDB for HBase SDK for Java is installed. For more information, see Install ApsaraDB for HBase SDK for Java.
Procedure
Create a configuration object and configure the endpoint, username, and password used to connect to LindormTable.
// Create a configuration object. Configuration conf = HBaseConfiguration.create(); Specify the endpoint used to connect to LindormTable by using ApsaraDB for HBase SDK for Java. 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");Parameter
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.
ImportantIf your application is deployed on an Elastic Compute Service (ECS) instance, we recommend that you connect to the Lindorm instance over a Virtual Private Cloud (VPC) for higher security and lower network latency. For more information about how to obtain the LindormTable endpoint for VPC, see View endpoints.
If your application is deployed on a local server and needs to connect to the Lindorm instance over the Internet, you can perform the following steps to enable the public endpoint for the instance in the Lindorm console: In the left-side navigation pane, select . On the Wide Table Engine tab, click Enable Public Endpoint.
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
Use the configuration object created in Step 1 to establish a connection.
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. // If you create a table in a production environment, we recommend that you partition the table in advance based on the data characteristics. admin.createTable(htd); // 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 within 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. System.out.println(result.toString()); } scanner.close(); }