How to Use HBase Client to Access Alibaba Cloud NoSQL Table Store

Image for post
Image for post

Introduction

Apache HBase

Table Store

HBase Client

The Challenge

Usage

Code Location

Steps for Using HBase Client to Access Alibaba Cloud NoSQL Table Store

Step 1. Configure Project Dependencies

<dependencies>
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>tablestore-hbase-client</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>

Step 2. Configure files

<configuration>
<property>
<name>hbase.client.connection.impl</name>
<value>com.alicloud.tablestore.hbase.TablestoreConnection</value>
</property>
<property>
<name>tablestore.client.endpoint</name>
<value>endpoint</value>
</property>
<property>
<name>tablestore.client.instancename</name>
<value>instance_name</value>
</property>
<property>
<name>tablestore.client.accesskeyid</name>
<value>access_key_id</value>
</property>
<property>
<name>tablestore.client.accesskeysecret</name>
<value>access_key_secret</value>
</property>
<property>
<name>hbase.client.tablestore.family</name>
<value>f1</value>
</property>
<property>
<name>hbase.client.tablestore.table</name>
<value>ots_adaptor</value>
</property>
</configuration>

Step 3. Connect to Table Store

Configuration config = HBaseConfiguration.create();        // Create a Table Store connection
Connection connection = ConnectionFactory.createConnection(config);
// Admin is in charge of creating, managing and deleting connections.
Admin admin = connection.getAdmin();

Step 4. Create a Table

// Create an HTableDescriptor with only one column family
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
// Create a column family. Use default values for MaxVersion and TimeToLive. The default value of MaxVersion is 1 and the default value of TimeToLive is Integer.INF_MAX.
descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));
// Create a table using the Admin createTable interface
System.out.println("Create table " + descriptor.getNameAsString());
admin.createTable(descriptor);

Step 5. Write Data

// Create a Table Store table for reading, writing, updating and deleting operations on a single table
Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
// Create a Put object with the primary key as row_1
System.out.println("Write one row to the table");
Put put = new Put(ROW_KEY);
// Add a column. Table Store only supports single column families. The column family name is configured in hbase-site.xml. If it is not configured, the default value of "f" applies. Therefore, you can leave the COLUMN_FAMILY_NAME null when writing data.
put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, COLUMN_VALUE);
// Execute the table put operation. Use the HBase API to write the row of data to Table Store.
table.put(put);

Step 6. Read Data

// Create a Get object and read the row with a primary key of ROW_KEY
Result getResult = table.get(new Get(ROW_KEY));
Result result = table.get(get);
// Prints the results
String value = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));
System.out.println("Get one row by row key");
System.out.printf("\t%s = %s\n", Bytes.toString(ROW_KEY), value);

Step 7. Scan Data

Scan data in all the rows in the table.
System.out.println("Scan for all rows:");
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan); // Print the result in cycles
for (Result row : scanner) {
byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
System.out.println('\t' + Bytes.toString(valueBytes));
}

Step 8. Delete a Table

print("Delete the table");
admin.disableTable(table.getName());
admin.deleteTable(table.getName());

Conclusion

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store