This topic describes how to use the ApsaraDB for HBase API for Java to connect to and use the wide table engine LindormTable.
Prerequisites
- Java Development Kit (JDK) 1.8 or later is installed.
- The endpoint that corresponds to Access by Using HBase Java API on the Wide Table Engine tab is obtained. For more information, see View endpoints.
- The IP address of your client is added to the allowlist of the Lindorm instance. For information about how to add an IP address to an allowlist of a Lindorm instance, see Configure a whitelist.
- ApsaraDB for HBase SDK for Java is installed. For more information, see Upgrade 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 of the Lindorm instance to which you want to connect. You can view the endpoint on the Database Connections page in 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 username and password. The default username and password are root. You can specify the username and password based on your business requirements. --> <property> <name>hbase.client.username</name> <value>root</value> </property> <property> <name>hbase.client.password</name> <value>root</value> </property> <!-- If you add the alihbase-client as a dependency in your code, you do not need to set the connection.impl parameter. If you add the alihbase-connector as a dependency in your code, you must set the connection.impl parameter. --> <!--property> <name>hbase.client.connection.impl</name> <value>org.apache.hadoop.hbase.client.AliHBaseUEClusterConnection</value> </property--> </configuration>
- Use the following sample code to create a Configuration object and add configuration items to the Configuration object:
// Create a Configuration object. Configuration conf = HBaseConfiguration.create(); // Specify the endpoint of the Lindorm instance to which you want to connect. You can view the endpoint on the Database Connections page in 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 VPC, use the VPC endpoint. conf.set("hbase.zookeeper.quorum", "ld-xxxx-proxy-hbaseue.hbaseue.xxx.rds.aliyuncs.com:30020"); // Specify the username and password. The default username and password are root. You can specify the username and password based on your business requirements. conf.set("hbase.client.username", "root") conf.set("hbase.client.password", "root") // If you add a Lindorm client as a dependency in your code, you do not need to set the connection.impl parameter. If you add the alihbase-connector as a dependency in your code, you must set the connection.impl parameter. //conf.set("hbase.client.connection.impl", AliHBaseUEClusterConnection.class.getName());
Parameter Example value Method to obtain the parameter value host:port ld-bp17j28j2y7pm****-proxy-lindorm-pub.lindorm.rds.aliyuncs.com:30020 You can obtain the endpoint that corresponds to Access by Using HBase Java API on the Wide Table Engine tab. For more information, see View endpoints. Username root If you forget the password that corresponds to the username, you can reset the password in the Lindorm Insight system of LindormTable. For more information, see Change the password of a user. Password root Important If you add the alihbase-connector earlier than V1.0.9 or V2.0.9 as a dependency, the parameter settings are different.
- Add the following configuration items to the configuration file hbase-site.xml:
- Establish a connection between the Lindorm client and the database.
Connection connection = ConnectionFactory.createConnection(conf);
Note The 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 based on the data characteristics. admin.createTable(htd); // Disable a table. admin.disableTable(TableName.valueOf("tablename")); // Truncate a table. admin.truncateTable(TableName.valueOf("tablename"), true); // Delete a table. You must execute the DISABLE statement before you delete a table. 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(); }
- DDL operations