This topic describes how to use the Java API provided by ApsaraDB for HBase to access an ApsaraDB for HBase Performance-enhanced Edition instance.
Prerequisites
ApsaraDB for HBase SDK for Java is installed. For more information, see Download ApsaraDB for HBase SDK for Java.
If you connect to an ApsaraDB for HBase Performance-enhanced Edition instance over an internal network, make sure that an Elastic Compute Service (ECS) instance is purchased. This ECS instance must be deployed in the same virtual private cloud (VPC) as the ApsaraDB for HBase Performance-enhanced Edition instance. Make sure that the IP address of the ECS instance is in the whitelist of the ApsaraDB for HBase Performance-enhanced Edition instance.
For more information about how to purchase an ECS instance, see Create an ECS instance.
For more information about how to configure an IP address whitelist for an ApsaraDB for HBase Performance-enhanced Edition instance, see Configure a whitelist.
If you connect to an ApsaraDB for HBase Performance-enhanced Edition instance over the Internet, make sure that an IP address whitelist is configured for the instance. For more information, see Configure a whitelist.
Copy the client connection configuration.
Log on to the ApsaraDB for HBase console.
On the Clusters page, find the instance that you want to manage and click the instance ID.

In the left-side navigation pane of the instance details page, click Database Connection.
On the Database Connection page, click Generate Configuration Items. In the Client Connection Configuration dialog box, click the Java Configuration tab, and copy the Java code.

Initialize the client. Paste the copied Java configuration code to your Java project file to create a client.
// Create a Configuration object as the client. Configuration conf = HBaseConfiguration.create(); // Specify the endpoint of your ApsaraDB for HBase instance. In the ApsaraDB for HBase console, navigate to the instance details page and click Database Connection. In the Connection Information section, you can obtain the public endpoint or the VPC-facing endpoint. If you connect to the instance over the Internet, use the public endpoint. If you connect to the instance over a VPC, use the VPC-facing endpoint. conf.set("hbase.zookeeper.quorum", "ld-xxxx-proxy-hbaseue.hbaseue.xxx.rds.aliyuncs.com:30020"); // Specify the username and the password that are used to access the instance. By default, the username and the password are root. You can change them as needed. conf.set("hbase.client.username", "root"); conf.set("hbase.client.password", "root"); // If you add the alihbase-client dependency in your development project, you do not need to configure the connection.impl parameter. If you add the alihbase-connector dependency in your development project, you must configure the connection.impl parameter. //conf.set("hbase.client.connection.impl", AliHBaseUEClusterConnection.class.getName());Create a connection to the ApsaraDB for HBase instance.
Create a Connection object to connect the Configuration object to the ApsaraDB for HBase instance.
// Create a connection to the ApsaraDB for HBase instance. You need to create only one connection within the client lifecycle. The connection is thread secured and can be shared by all the threads. // After the client completes a workload, the Connection object must be closed to prevent connection leaks. // You can also execute the try finally statement to prevent connection leaks. Connection connection = ConnectionFactory.createConnection(conf);After the connection is established, you can use the ApsaraDB for HBase Java API to access the ApsaraDB for HBase Performance-enhanced Edition instance. The following section provides examples in the Java format:
DDL statements
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. // We recommend that you pre-split the table based on the data to be stored when you create the table in the production environment. admin.createTable(htd); // Disable a table. admin.disableTable(TableName.valueOf("tablename")); // Truncate a table. admin.truncateTable(TableName.valueOf("tablename"), true); // Delete a table. admin.deleteTable(TableName.valueOf("tablename")); }DML statements
// A table is a non-thread-safe object. When a thread performs operations on a table, the thread must obtain the required table object from the connection. 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. // ... } scanner.close(); }