This topic describes how to use the HBase Java API to access an ApsaraDB for HBase Performance-enhanced Edition cluster.

Prerequisites

  • HBase SDK for Java is installed. For more information, see Download HBase SDK for Java.
  • If you connect to an ApsaraDB for HBase Performance-enhanced Edition cluster 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 cluster. The IP address of the ECS instance must be added to the whitelist of the cluster.
  • If you connect to an ApsaraDB for HBase Performance-enhanced Edition cluster over the Internet, make sure that a whitelist is configured for the cluster. For more information, see Configure a whitelist.
  1. Copy the client connection configuration.
    1. Log on to the ApsaraDB for HBase console.
    2. On the Clusters page, find the cluster that you want to manage and click the name of the cluster. The details page of the cluster appears.
      Cluster
    3. In the left-side navigation pane of the cluster details page, click Database Connection. The Database Connection page appears.
    4. Click Generate Configuration Items. In the Client Connection Configuration dialog box, click the Java Configuration tab, and copy the Java code.
      ClientConnectionconfigration
  2. Initialize an HBase configuration and add the copied Java configuration code.
    // Create a configuration.
    Configuration conf = HBaseConfiguration.create();
    // Specify the endpoint of the cluster. You can obtain the endpoint on the Database Connection page of the cluster in the console. Select the public endpoint or the VPC-facing endpoint based on your business requirements.
    conf.set("hbase.zookeeper.quorum", "ld-xxxx-proxy-hbaseue.hbaseue.xxx.rds.aliyuncs.com:30020");
    // Specify the username and the password. The default username and password are both root. You can change them based on your business requirements.
    conf.set("hbase.client.username", "root")
    conf.set("hbase.client.password", "root")
    
    // If you add the ApsaraDB for HBase client as a dependency, you do not need to configure the connection.impl parameter. If you add the alihbase-connector plug-in as a dependency, you must configure the connection.impl parameter.
    //conf.set("hbase.client.connection.impl", AliHBaseUEClusterConnection.class.getName());
  3. Create an HBase connection.

    In the application code that runs in an on-premises environment, create a connection based on the configuration.

    // Create an HBase connection. You need only to perform this step once during the lifecycle of the program. The thread of this connection is secure and can be shared among all threads.
    // Close the connection object after the program ends. Otherwise, a connection leak may occur.
    // You can also execute a try-finally statement to prevent leakage.
    Connection connection = ConnectionFactory.createConnection(conf); 
  4. After the connection is established, you can use the HBase Java API to access the ApsaraDB for HBase Performance-enhanced Edition cluster. The following section provides examples in the Java format:
    • Data Definition Language (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 a table based on the data 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);
    • Data Manipulation Language (DML) statements
      // A table is a non-thread secure object. When a thread performs operations on a table, it must obtain the corresponding 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 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 query result.
              // ...
              }
          scanner.close();
      }