Access HBase with a Java client

更新时间:
复制 MD 格式

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

Prerequisites

Background information

  • You can access an ApsaraDB for HBase Standard Edition cluster using an open source HBase community client or the custom client provided by Alibaba Cloud.

  • If your runtime environment lacks the JAR packages required for the HBase client, add the following Maven configuration to bundle all client dependencies into the final JAR package.

      <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 cluster.

    1. Log on to the ApsaraDB for HBase console.

    2. On the Clusters page, click the name of your Standard Edition instance to open the cluster details page.

    3. On the cluster details page, click Database Connection to open the Database Connection page.

      On the Database Connection page, locate the ZooKeeper Endpoints (VPC) row. This row lists the addresses of the three master nodes, formatted as hb-<InstanceID>-master<N>-001.hbase.rds.aliyuncs.com:2181 (where N is 1, 2, or 3). Copy these addresses to configure your connection.

  2. Configure the ZooKeeper endpoints to connect to the cluster.

    In your application code, use the following configuration to connect to the cluster.

    Note

    Replace the placeholder ZooKeeper endpoints in the sample code with your cluster's actual endpoints. The example demonstrates creating a table, writing data, and reading data.

     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();
                            // The ZooKeeper endpoints for the cluster.
         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();
                 }
             }
         }
     }

Next steps

You can also download a sample Java project. After you download the project, replace the placeholder value for the ZooKeeper variable with your cluster's endpoints.