You can configure the client parameters to access ApsaraDB for HBase Performance-enhanced Edition in the following ways.

  1. Install the SDK for Java. For more information, see Install the SDK for Java.
  2. Retrieve the endpoint of the cluster. For more information, see Connect to a cluster.

Specify client parameters

Method 1: Specify parameters by modifying the configuration file.

Add the following configurations to hbase-site.xml:

<configuration>
      <! --
    The public endpoint or Virtual Private Cloud (VPC) internal endpoint used to connect to the cluster. You can retrieve the endpoint on the Database Connection page in the console.
    -->
    <property>
        <name>hbase.zookeeper.quorum</name>
        <value>ld-xxxx-proxy-hbaseue.hbaseue.xxx.rds.aliyuncs.com:30020</value>
    </property>
    <! --
    By default, both the username and password values are set to root. You can change the username and the password as needed.
    -->
    <property>
        <name>hbase.client.username</name>
        <value>root</value>
    </property>
    <property>
        <name>hbase.client.password</name>
        <value>root</value>
    </property>
    <! --
    If you use the ApsaraDB for HBase client, you do not need to specify the connection.impl parameter. If you use alihbase-connector, you must specify this parameter.
    -->
    <! --property>
        <name>hbase.client.connection.impl</name>
        <value>org.apache.hadoop.hbase.client.AliHBaseUEClusterConnection</value>
    </property-->
</configuration>

Method 2: Specify parameters by running a program.

Create a Configuration object and specify the related parameters by running the following program.

// Create a Configuration object.
Configuration conf = HBaseConfiguration.create();
// The public endpoint or VPC internal endpoint used to connect to the cluster. You can retrieve the endpoint on the Database Connection page in the console.
conf.set("hbase.zookeeper.quorum", "ld-xxxx-proxy-hbaseue.hbaseue.xxx.rds.aliyuncs.com:30020");
// By default, both the username and password values are set to root. You can change the username and the password as needed.
conf.set("hbase.client.username", "root")
conf.set("hbase.client.password", "root")

// If you use the Alibaba Cloud HBase client, you do not need to specify the connection.impl parameter. If you use alihbase-connector, you must specify this parameter.
//conf.set("hbase.client.connection.impl", AliHBaseUEClusterConnection.class.getName());

Note: If you add the alihbase-connector earlier than version 1.0.9 or 2.0.9 as a dependency, the configurations are different. For more information, see Use the Java API to access ApsaraDB for HBase (former version).

Create a connection

Use the Configuration object conf to create a Connection object. For more information about how to create the conf, see the sample code in the previous step.

// Create a connection to ApsaraDB for HBase. You only need to perform this task once while the program is running. This thread is secure and can be shared with all other threads.
// Close the Connection object after the program stops. Otherwise, more connections will be created, resulting in a waste of resources.
// You can also use try finally to avoid creating new connections.
Connection connection = ConnectionFactory.createConnection(conf);

Use the Java API

After you create the connection, you can use the Java API to connect to the ApsaraDB for HBase Performance-enhanced Edition cluster. The following sample code shows how to call the API operations.

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.
    admin.createTable(htd);

    // Disable a table.
    admin.disableTable(TableName.valueOf("tablename"));

    // Truncate a table.
    admin.truncateTable(TableName.valueOf("tablename"), true);

    // Delete a table.
    admin.deleteTable(TableName.valueOf("tablename"));
}

Data Manipulation Language (DML) statements

// A Table is a non-threaded secure object. A thread must retrieve a Table object from Connection before it can manage 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 the range of the data.
    Scan scan = new Scan(Bytes.toBytes("startRow"), Bytes.toBytes("endRow"));
    ResultScanner scanner = table.getScanner(scan);
    for (Result result : scanner) {
        // Handle the query result.
        // ...
    }
    scanner.close();
}