edit-icon download-icon

Table-level operations

Last Updated: Mar 20, 2018

The SDK offers table operation interfaces, such as CreateTable, ListTable, UpdateTable, DescribeTable, and DeleteTable.

Note: The following operation examples are based on synchronous interfaces. See Asynchronous interfaces for more information on how to operate asynchronous interfaces.

Create a table (CreateTable)

You must always specify the name and primary key of the table you want to create. The primary key contains between 1 and 4 primary key columns, each of which has a name and type.

In Table Store tables, you can set auto increments in primary key columns. See Primary key column auto-increment for more information.

Example

In this example, the table name is simple_create_delete_table. The primary key only has one primary key column, named pkey, and is of an integer type (kPKT_Integer).

  1. CreateTableRequest req;
  2. {
  3. // immutable configurations of the table
  4. TableMeta& meta = req.mutableMeta();
  5. meta.mutableTableName() = "simple_create_delete_table";
  6. {
  7. // with exactly one integer primary key column
  8. Schema& schema = meta.mutableSchema();
  9. PrimaryKeyColumnSchema& pkColSchema = schema.append();
  10. pkColSchema.mutableName() = "pkey";
  11. pkColSchema.mutableType() = kPKT_Integer;
  12. }
  13. }
  14. CreateTableResponse resp;
  15. Optional<OTSError> res = client.createTable(resp, req);

Note: Detailed code is available at createTable@GitHub.

Variable parameters

You can set several variable parameters for the data table. Variable parameters can be set when creating tables, or changed using Update table.

Variable parameters include the following:

Variable parameter Name Default value
mutableTimeToLive() Time to live -1 (meaning data never expires)
mutableMaxVersions() Max Versions 1
mutableMaxTimeDeviation() Max Version Offset 86,400s (or one day)
mutableReservedThroughput() Reserved read/write throughput 0 (all read/write charged on a Pay-As-You-Go basis)

The following is an example of setting the reserved read/write throughput when creating a table:

  1. CreateTableRequest req;
  2. {
  3. // immutable configurations of the table
  4. TableMeta& meta = req.mutableMeta();
  5. meta.mutableTableName() = "create_table_with_reserved_throughput";
  6. {
  7. // with exactly one integer primary key column
  8. Schema& schema = meta.mutableSchema();
  9. PrimaryKeyColumnSchema& pkColSchema = schema.append();
  10. pkColSchema.mutableName() = "pkey";
  11. pkColSchema.mutableType() = kPKT_Integer;
  12. }
  13. }
  14. {
  15. TableOptions& opts = req.mutableOptions();
  16. {
  17. // 0 reserved read capacity-unit, 1 reserved write capacity-unit
  18. CapacityUnit cu(0, 1);
  19. opts.mutableReservedThroughput().reset(util::move(cu));
  20. }
  21. }
  22. CreateTableResponse resp;
  23. Optional<OTSError> res = client.createTable(resp, req);

List a table name (ListTable)

Used to get the names of all tables created in the current instance.

API

SyncClient::listTable() is used to list all tables under the instance.

  1. SyncClient* client = ...;
  2. ListTableRequest req;
  3. ListTableResponse resp;
  4. Optional<OTSError> res = client->listTable(resp, req);

Example

Used to get the names of all tables under the instance.

  1. const IVector<string>& xs = resp.tables();
  2. for(int64_t i = 0; i < xs.size(); ++i) {
  3. cout << xs[i] << endl;
  4. }

Note: Detailed code is available at listTable@GitHub.

Update a table (UpdateTable)

Updates the variable parameters of a specified table.

Example

Update the reserved throughput.

  1. UpdateTableRequest req;
  2. req.mutableTable() = "YourTable";
  3. UpdateTableResponse resp;
  4. {
  5. TableOptions& opts = req.mutableOptions();
  6. {
  7. // 0 reserved read capacity-unit, 1 reserved write capacity-unit
  8. CapacityUnit cu(0, 1);
  9. opts.mutableReservedThroughput().reset(util::move(cu));
  10. }
  11. }
  12. Optional<OTSError> res = client.updateTable(resp, req);

Note: Detailed code is available at updateTable@GitHub.

Query the table information (DescribeTable)

You can query the following table information through the describeTable() interface:

Information item Description
Table status Includes:
- kTS_Active: The table can offer normal read and write services.
- kTS_Inactive: The table cannot be read or written, but table data is reserved. This status usually occurs during a primary-backup table switch.
- kTS_Loading: The table is being created. The table cannot be read or written.
- kTS_Unloading: The table is being deleted. The table cannot be read or written.
- kTS_Updating: The variable table parameters are being updated. The table cannot be read or written.
Table meta See Create a table.
Variable table parameters See Variable parameters.
Split points between shards A Table Store table is split horizontally into several shards. Split points can be obtained through this interface.
Note: Table Store can be automatically split and merged in the background according to the load. As such, the split points received by this interface are guaranteed to reflect past shards, but not necessarily match what is currently going on.

Example

  1. DescribeTableRequest req;
  2. req.mutableTable() = "YourTable";
  3. DescribeTableResponse resp;
  4. Optional<OTSError> res = client.describeTable(resp, req);

Note: Detailed code is available at describeTable@GitHub.

Delete a table (DeleteTable)

Deletes specified tables under this instance. The only requirement is to specify the table name.

Example

  1. DeleteTableRequest req;
  2. req.mutableTable() = "YourTable";
  3. DeleteTableResponse resp;
  4. Optional<OTSError> res = client.deleteTable(resp, req);

Note: Detailed code is available at deleteTable@GitHub.

Thank you! We've received your feedback.