This topic describes how to use the HBase Java API to access an ApsaraDB for HBase Serverless Edition cluster.
Prerequisites
- The Java software development kit (SDK) is installed. For more information, see Download the HBase Java SDK.
- If you use an internal network connection, you must have an ECS instance in the same virtual private cloud (VPC) as your ApsaraDB for HBase Serverless Edition cluster. If you do not have an ECS instance, see Create an ECS instance.Note You cannot purchase new ApsaraDB for HBase Serverless Edition clusters.
Notes
ApsaraDB for HBase Serverless Edition supports access only from HBase V1.x and V2.x clients. These clients require the alihbase-connector plugin to support features such as security authentication and public network access.
Procedure
- Obtain the AccessKey ID and AccessKey secret. For more information, see Obtain and use the username and password for ApsaraDB for HBase Serverless Edition.
- Initialize the HBase configuration file.
// Create a Configuration object. Configuration conf = HBaseConfiguration.create(); // The endpoint of the instance. conf.set("hbase.zookeeper.quorum", "https://sh-****-hbase-serverless.hbase.rds.aliyuncs.com:443"); // Set the username and password. The format is AccessKey ID:AccessKey secret. Modify them as needed. conf.set("hbase.client.username", "AccessKey_ID"); conf.set("hbase.client.password", "AccessKey_Secret"); // 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()); - Create an HBase connection.
Create a Connection object from the Configuration object in your service code.
// Create an HBase connection. You only need to create the connection once during the program lifecycle. This connection is thread-safe and can be shared by all threads. // After the program ends, close the Connection object to prevent connection leaks. // You can also use a try-finally block to prevent leaks. Connection connection = ConnectionFactory.createConnection(conf); - After the connection is established, use the Java API to access the ApsaraDB for HBase Serverless Edition cluster. The following code provides examples of simple Java operations.
- 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. // When you create a table in a production environment, pre-partition 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 operations
// The Table object is not thread-safe. Each thread must obtain the corresponding Table object from the Connection object to perform operations on the table. 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(); }
- DDL operations
该文章对您有帮助吗?