Prerequisites

HBase SDK for Java is installed. For more information, see Install and upgrade HBase SDK for Java.

Configure a client to connect to a Lindorm cluster

You can use one of the following methods to configure a client. Then, you can use the client to connect to an ApsaraDB for Lindorm (Lindorm) cluster.

  • Add the following configuration to the configuration file hbase-site.xml:
    <configuration>
          <!--
        Enter the endpoint of your Lindorm cluster. You can obtain the value on the Database Connection page in the Lindorm console. Use a public endpoint if you connect to the Lindorm cluster over the Internet. Use an internal endpoint if you connect to the Lindorm cluster over a virtual private cloud (VPC).
        -->
        <property>
            <name>hbase.zookeeper.quorum</name>
            <value>ld-xxxx-proxy-hbaseue.lindormue.xxx.rds.aliyuncs.com:30020</value>
        </property>
        <!--
        Specify the username and the password. By default, the username and the password are root. You can change them 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 HBase client provided by Alibaba Cloud, you do not need to configure the connection.impl parameter. If you use the open source HBase client, you can connect to a Lindorm cluster only after alihbase-connector is installed. In this case, you must configure the connection.impl parameter.
        -->
        <!--property>
            <name>hbase.client.connection.impl</name>
            <value>org.apache.hadoop.hbase.client.AliHBaseUEClusterConnection</value>
        </property-->
    </configuration>
  • Use the following code to create a configuration class and add configuration items to the configuration class:
    // Create a configuration class. 
    Configuration conf = HBaseConfiguration.create();
    // Enter the endpoint of your Lindorm cluster. You can obtain the value on the Database Connection page in the Lindorm console. Use a public endpoint if you connect to the Lindorm cluster over the Internet. Use an internal endpoint if you connect to the Lindorm cluster over a virtual private cloud (VPC). 
    conf.set("hbase.zookeeper.quorum", "ld-xxxx-proxy-hbaseue.hbaseue.xxx.rds.aliyuncs.com:30020");
    // Specify the username and the password. By default, the username and the password are root. You can change them as needed. 
    conf.set("hbase.client.username", "root")
    conf.set("hbase.client.password", "root")
    
    // If you use the HBase client provided by Alibaba Cloud, you do not need to configure the connection.impl parameter. If you use the open source HBase client, you can connect to a Lindorm cluster only after alihbase-connector is installed. In this case, you must configure the connection.impl parameter. 
    //conf.set("hbase.client.connection.impl", AliHBaseUEClusterConnection.class.getName());
    Notice If you use alihbase-connector earlier than V1.0.9 or V2.0.9, the configuration is different.

Establish a connection

If you want to establish a connection, create a configuration class for the connection. For information about how to create a configuration class, see the description in the preceding step.
// Establish a connection in HBase. Perform this operation once during the lifecycle of a connection. The system enables thread safety. This indicates that a thread can be used by other threads. // After the connection ends, you must close the connection objects to prevent a connection leak. You can also execute the try finally statement to prevent a connection leak. 
Connection connection = ConnectionFactory.createConnection(conf);

Use the HBase API for Java

After you establish a connection, you can use the HBase API for Java to connect to a Lindorm cluster. You can use the following sample Java code.

  • 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.
        // If you create a table in a production environment, we recommend that you partition the 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-thread-safe object. When a thread performs operations on a table, the thread must obtain the required 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();
    }