This topic describes how to access an ApsaraDB for HBase Performance-enhanced Edition instance using the ApsaraDB for HBase Java API.
Prerequisites
The Java SDK is installed. For more information, see Upgrade the HBase Java SDK.
If your application is deployed on an ECS instance, ensure the ApsaraDB for HBase Performance-enhanced Edition instance and the ECS instance meet the following conditions for network connectivity. For information about how to view ECS instance details, see View instance information.
The instances are in the same region. We recommend placing them in the same availability zone to reduce network latency.
The instances use the same network type.
NoteA VPC provides higher security. We recommend that you use a VPC. If the network type is VPC, ensure that both instances are in the same VPC.
The IP address of the ECS instance or the client has been added to the ApsaraDB for HBase Performance-enhanced Edition instance's whitelist. For more information, see Configure a whitelist.
Procedure
Initialize the HBase configuration. In your project, enter the following code and configure the parameters as described in the table below.
// Create a Configuration object. Configuration conf = HBaseConfiguration.create(); // The Java API endpoint of the cluster. You can obtain the endpoint on the Database Connection page on the console. conf.set("hbase.zookeeper.quorum", "ld-bp150tns0sjxs****-proxy-hbaseue.hbaseue.rds.aliyuncs.com:30020"); // Set the username and password. conf.set("hbase.client.username", "testuser"); conf.set("hbase.client.password", "password"); // If you directly depend on the Alibaba Cloud HBase client, you do not need to configure the connection.impl parameter. If you depend on alihbase-connector, you must configure this parameter. //conf.set("hbase.client.connection.impl", AliHBaseUEClusterConnection.class.getName());Parameter
Value
Description
host:port
ld-bp150tns0sjxs****-proxy-hbaseue.hbaseue.rds.aliyuncs.com:30020
The connection string for the Java API endpoint of your ApsaraDB for HBase Performance-enhanced Edition instance. To obtain this value, go to the ApsaraDB for HBase console and click the target instance ID. In the left-side navigation pane, click Database Connection. In the Connection Information section, find the Java API endpoint.
username
testuser
If you forget your username or password, you can reset it in the ApsaraDB for HBase cluster management system. For more information, see Manage users.
password
password
Create a connection to the ApsaraDB for HBase Performance-enhanced Edition instance.
Connection connection = ConnectionFactory.createConnection(conf);NoteCreate the connection only once during the application lifecycle. The connection is thread-safe and can be shared among all threads. To prevent connection leaks, close the Connection object after the program finishes. You can also use a try-with-resources statement or a
try...finallyblock to prevent leaks.Once connected, you can use the Java API to access the ApsaraDB for HBase Performance-enhanced Edition instance. The following are simple Java examples.
NoteTo ensure compatibility with HBase 1.x clients, this example uses the syntax for an HBase 1.x client. If you use an HBase 2.x client, some IDEs may show
Deprecatedwarnings, but this does not affect its operation.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 with a single partition. // In production environments, we recommend pre-splitting the table based on your data characteristics. admin.createTable(htd); // Disable the table. admin.disableTable(TableName.valueOf("tablename")); // Truncate the table. admin.truncateTable(TableName.valueOf("tablename"), true); // Delete the table. admin.deleteTable(TableName.valueOf("tablename")); }DML statements
// The Table object is not thread-safe. Each thread must obtain a unique 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. 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(); }