All Products
Document Center

Use the Java client to access ApsaraDB for HBase

Last Updated: May 31, 2020

Use the Java client to access ApsaraDB for HBase


You need to create an Elastic Compute Service (ECS) instance in the region where ApsaraDB for HBase is deployed. If the ECS instance has been created, go to the next step. If the ECS instance has not been created, you can purchase a pay-by-data-transfer ECS instance.For more information, see Preparation.

You can also access ApsaraDB for HBase over the Internet. For more information, see Solutions for accessing ApsaraDB for HBase over a public network.

Use a client to read/write data of ApsaraDB for HBase

You can use the open source client or the customized HBase client provided by Alibaba Cloud to access ApsaraDB for HBase Standard Edition. Note that if you access the service over the Internet, you must use the client provided by Alibaba Cloud.For more information about how to download and use the client, see Download ApsaraDB for HBase clients.

If you do not have the required JAR file in your runtime environment, you must add the following shade configuration in Maven. This allows you to ensure that the JAR file includes all dependencies that are required by the client.

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-shade-plugin</artifactId>
  6. <version>3.0.0</version>
  7. <executions>
  8. <execution>
  9. <phase>package</phase>
  10. <goals>
  11. <goal>shade</goal>
  12. </goals>
  13. <configuration>
  14. <artifactSet>
  15. </artifactSet>
  16. </configuration>
  17. </execution>
  18. </executions>
  19. </plugin>
  20. </plugins>
  21. </build>
  1. Retrieve the ZooKeeper (ZK) address of the cluster.

    Log on to the HBase console, click the cluster ID, and then you can see the ZK address in the Connection Information section of the Database Connection page, as shown in the following section.


    Note: For more information about how to retrieve the endpoint of ApsaraDB for HBase Performance-enhanced Edition, see Use the Java API to access ApsaraDB for HBase (119570).

  2. Configure the ZK address to connect to the cluster.

    Replace the ZK address in the code with the ZK address of the cluster. Then, you can run the following sample code to access the HBase cluster. The sample code shows how to create tables, read data, and write data.

  1. private static final String TABLE_NAME = "mytable";
  2. private static final String CF_DEFAULT = "cf";
  3. public static final byte[] QUALIFIER = "col1".getBytes();
  4. private static final byte[] ROWKEY = "rowkey1".getBytes();
  5. public static void main(String[] args) {
  6. Configuration config = HBaseConfiguration.create();
  7. String zkAddress = ",,";
  8. config.set(HConstants.ZOOKEEPER_QUORUM, zkAddress);
  9. Connection connection = null;
  10. try {
  11. connection = ConnectionFactory.createConnection(config);
  12. HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
  13. tableDescriptor.addFamily(new HColumnDescriptor(CF_DEFAULT));
  14. System.out.print("Creating table. ");
  15. Admin admin = connection.getAdmin();
  16. admin.createTable(tableDescriptor);
  17. System.out.println(" Done.") ;
  18. Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
  19. try {
  20. Put put = new Put(ROWKEY);
  21. put.addColumn(CF_DEFAULT.getBytes(), QUALIFIER, "this is value".getBytes());
  22. table.put(put);
  23. Get get = new Get(ROWKEY);
  24. Result r = table.get(get);
  25. byte[] b = r.getValue(CF_DEFAULT.getBytes(), QUALIFIER); // returns current version of value
  26. System.out.println(new String(b));
  27. } finally {
  28. if (table ! = null) table.close();
  29. }
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. } finally {
  33. if (connection ! = null) {
  34. try {
  35. connection.close();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41. }

Sample code

You can also download the Java code provided by Alibaba Cloud, and replace the ZK variable.

Click here to download the sample code used to access ApsaraDB for HBase.