This topic describes how to use a Java client to access an ApsaraDB for HBase Standard Edition cluster.

Prerequisites

  • If you connect to an ApsaraDB for HBase Standard 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.
    • 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 Standard Edition cluster, see Configure a whitelist.
  • If you connect to an ApsaraDB for HBase Standard Edition cluster over the Internet, you must first use ApsaraDB for HBase SDK for Java to create a client. For more information about how to download the SDK, see Download ApsaraDB for HBase SDK for Java.

    Make sure that an IP address whitelist is configured to allow access to the ApsaraDB for HBase Standard Edition cluster. For more information, see Configure a whitelist.

Background information

  • To connect to an ApsaraDB for HBase Standard Edition cluster, you can use the client provided by HBase Community Edition or use ApsaraDB for HBase SDK for Java to create a client.
  • If you do not have the required JAR file in your runtime environment, you must add the following configuration to your Maven project. This ensures that the JAR file contains the dependencies that are required by the HBase client.
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                  <artifactSet>
                  </artifactSet>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>

Procedure

  1. Obtain the ZooKeeper endpoints of your ApsaraDB for HBase Standard Edition cluster.
    1. Log on to the ApsaraDB for HBase console.
    2. On the Clusters page, find your ApsaraDB for HBase Standard Edition cluster and click the cluster ID. The details page of the cluster appears.
    3. In the left-side navigation pane of the cluster details page, click Database Connection. On the page that appears, copy the ZooKeeper endpoints of the cluster based on your business requirements.
      uplode
  2. Configure the ZooKeeper endpoints to connect to the cluster.

    The following code provides an example on how to configure the ZooKeeper endpoints to connect to the cluster.

    Note Replace the value of zkAddress in the following sample code with the ZooKeeper endpoints of the cluster. This way, you can use the sample code to access the ApsaraDB for HBase Standard Edition cluster. In the following example, a table is created and data is written and read.
     private static final String TABLE_NAME = "mytable";
     private static final String CF_DEFAULT = "cf";
     public static final byte[] QUALIFIER = "col1".getBytes();
     private static final byte[] ROWKEY = "rowkey1".getBytes();
    
     public static void main(String[] args) {
         Configuration config = HBaseConfiguration.create();
                            // ZooKeeper endpoints
         String zkAddress = "hb-bp1f5xxxx48a0r17i-001.hbase.rds.aliyuncs.com:2181,hb-bp1f5xxxx48a0r17i-002.hbase.rds.aliyuncs.com:2181,hb-bp1f5xxxx48a0r17i-003.hbase.rds.aliyuncs.com:2181";
         config.set(HConstants.ZOOKEEPER_QUORUM, zkAddress);
         Connection connection = null;
    
         try {
             connection = ConnectionFactory.createConnection(config);
    
             HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
             tableDescriptor.addFamily(new HColumnDescriptor(CF_DEFAULT));
             System.out.print("Creating table. ");
             Admin admin = connection.getAdmin();
             admin.createTable(tableDescriptor);
             System.out.println(" Done.");
             Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
             try {
                 Put put = new Put(ROWKEY);
                 put.addColumn(CF_DEFAULT.getBytes(), QUALIFIER, "this is value".getBytes());
                 table.put(put);
                 Get get = new Get(ROWKEY);
                 Result r = table.get(get);
                 byte[] b = r.getValue(CF_DEFAULT.getBytes(), QUALIFIER);  // returns current version of value
                 System.out.println(new String(b));
             } finally {
                 if (table != null) table.close();
             }
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             if (connection != null) {
                 try {
                     connection.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
     }

What to do next

You can also download the Java code-based project provided by Alibaba Cloud. In the Java code, replace the value of zkAddress with the ZooKeeper endpoints of your ApsaraDB for HBase Standard Edition cluster. Then, you can use this code to access the cluster.