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


Apache HBase

Table Store

HBase Client

The Challenge


Code Location

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

Step 1. Configure Project Dependencies


Step 2. Configure files


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());

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.
// Execute the table put operation. Use the HBase API to write the row of data to Table Store.

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");




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
Alibaba Cloud

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