edit-icon download-icon

Hello World

Last Updated: Mar 20, 2018

This topic describes how to use TableStore HBase Client to implement a simple Hello World program, and includes the following operations:

  • Configure project dependencies
  • Connect Table Store
  • Create a table
  • Write data
  • Read data
  • Scan data
  • Delete a table

Code position

This sample program uses HBase APIs to access Table Store. The complete sample program is located in the Github aliyun-tablestore-hbase-client project. The directory is src/test/java/samples/HelloWorld.java.

Use HBase API

Configure project dependencies

Configure Maven dependencies as follows.

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.aliyun.openservices</groupId>
  4. <artifactId>tablestore-hbase-client</artifactId>
  5. <version>1.2.0</version>
  6. </dependency>
  7. </dependencies>

For more information about advanced configurations, see Migrate from HBase to Table Store.

Configure the file

Add the following configuration items to hbase-site.xml.

  1. <configuration>
  2. <property>
  3. <name>hbase.client.connection.impl</name>
  4. <value>com.alicloud.tablestore.hbase.TablestoreConnection</value>
  5. </property>
  6. <property>
  7. <name>tablestore.client.endpoint</name>
  8. <value>endpoint</value>
  9. </property>
  10. <property>
  11. <name>tablestore.client.instancename</name>
  12. <value>instance_name</value>
  13. </property>
  14. <property>
  15. <name>tablestore.client.accesskeyid</name>
  16. <value>access_key_id</value>
  17. </property>
  18. <property>
  19. <name>tablestore.client.accesskeysecret</name>
  20. <value>access_key_secret</value>
  21. </property>
  22. <property>
  23. <name>hbase.client.tablestore.family</name>
  24. <value>f1</value>
  25. </property>
  26. <property>
  27. <name>hbase.client.tablestore.table</name>
  28. <value>ots_adaptor</value>
  29. </property>
  30. </configuration>

For more information about advanced configurations, see Migrate from HBase to Table Store.

Connect Table Store

Create a TableStoreConnection object to connect Table Store.

  1. Configuration config = HBaseConfiguration.create();
  2. // Create a Tablestore Connection
  3. Connection connection = ConnectionFactory.createConnection(config);
  4. // Admin is used for creation, management, and deletion
  5. Admin admin = connection.getAdmin();

Create a table

Create a table using the specified table name. Use the default table name for Max Versions and TimeToLive.

  1. // Create an HTableDescriptor, which contains only one ColumnFamily
  2. HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
  3. // Create a ColumnFamily. Use the default ColumnFamily name for Max Versions and TimeToLive. The default ColumnFamily name for Max Versions is 1 and for TimeToLive is Integer.INF_MAX
  4. descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));
  5. // Use the createTable API of the Admin to create a table
  6. System.out.println("Create table " + descriptor.getNameAsString());
  7. admin.createTable(descriptor);

Write data

Write a row of data to Table Store.

  1. // Create a TablestoreTable for reading, writing, updating, deletion, and other operations on a single table
  2. Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
  3. // Create a Put object with the primary key row_1
  4. System.out.println("Write one row to the table");
  5. Put put = new Put(ROW_KEY);
  6. // Add a column. Table Store supports only single ColumnFamilies. The ColumnFamily name is configured in hbase-site.xml. If the ColumnFamily name is not configured, the default name is "f". In this case, the value of COLUMN_FAMILY_NAME may be null when data is written.
  7. put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, COLUMN_VALUE);
  8. // Run put for Table, and use HBase APIs to write the row of data to Table Store
  9. table.put(put);

Read data

Read data of the specified row.

  1. // Create a Get object to read the row whose primary key is ROW_KEY.
  2. Result getResult = table.get(new Get(ROW_KEY));
  3. Result result = table.get(get);
  4. // Print the results
  5. String value = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));
  6. System.out.println("Get one row by row key");
  7. System.out.printf("\t%s = %s\n", Bytes.toString(ROW_KEY), value);

Scan data

Read data in the specified range.

  1. // Scan data of all rows in the table
  2. System.out.println("Scan for all rows:");
  3. Scan scan = new Scan();
  4. ResultScanner scanner = table.getScanner(scan);
  5. // Print the results cyclically
  6. for (Result row : scanner) {
  7. byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
  8. System.out.println('\t' + Bytes.toString(valueBytes));
  9. }

Delete a table

Use Admin APIs to delete a table.

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

Complete code

  1. package samples;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.HColumnDescriptor;
  5. import org.apache.hadoop.hbase.HTableDescriptor;
  6. import org.apache.hadoop.hbase.TableName;
  7. import org.apache.hadoop.hbase.client.*;
  8. import org.apache.hadoop.hbase.util.Bytes;
  9. import java.io.IOException;
  10. public class HelloWorld {
  11. private static final byte[] TABLE_NAME = Bytes.toBytes("HelloTablestore");
  12. private static final byte[] ROW_KEY = Bytes.toBytes("row_1");
  13. private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("f");
  14. private static final byte[] COLUMN_NAME = Bytes.toBytes("col_1");
  15. private static final byte[] COLUMN_VALUE = Bytes.toBytes("col_value");
  16. public static void main(String[] args) {
  17. helloWorld();
  18. }
  19. private static void helloWorld() {
  20. try {
  21. Configuration config = HBaseConfiguration.create();
  22. Connection connection = ConnectionFactory.createConnection(config);
  23. Admin admin = connection.getAdmin();
  24. HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
  25. descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));
  26. System.out.println("Create table " + descriptor.getNameAsString());
  27. admin.createTable(descriptor);
  28. Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
  29. System.out.println("Write one row to the table");
  30. Put put = new Put(ROW_KEY);
  31. put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, COLUMN_VALUE);
  32. table.put(put);
  33. Result getResult = table.get(new Get(ROW_KEY));
  34. String value = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));
  35. System.out.println("Get a one row by row key");
  36. System.out.printf("\t%s = %s\n", Bytes.toString(ROW_KEY), value);
  37. Scan scan = new Scan();
  38. System.out.println("Scan for all rows:");
  39. ResultScanner scanner = table.getScanner(scan);
  40. for (Result row : scanner) {
  41. byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
  42. System.out.println('\t' + Bytes.toString(valueBytes));
  43. }
  44. System.out.println("Delete the table");
  45. admin.disableTable(table.getName());
  46. admin.deleteTable(table.getName());
  47. table.close();
  48. admin.close();
  49. connection.close();
  50. } catch (IOException e) {
  51. System.err.println("Exception while running HelloTablestore: " + e.toString());
  52. System.exit(1);
  53. }
  54. }
  55. }
Thank you! We've received your feedback.