edit-icon download-icon

Table-level operations

Last Updated: Apr 08, 2018

The Table Store SDK provides the following APIs for operating tables: CreateTable, ListTable, DeleteTable, UpdateTable, and DescribeTable.

CreateTable

Creates a table based on the given table structure information.

When creating a table in Table Store, you must specify the table’s primary keys. A primary key contains one to four primary key columns. Each primary key column has a name and a type.

API

  1. // Description: This operation creates a table based on the entered table information
  2. // request is the instance of the CreateTableRequest class, which contains tableMeta, TableOption, and ReservedThroughput
  3. // For more information, see the documentation for the TableMeta class. After a table is created, it generally takes one minute to load the partitions
  4. // After that, you can perform table operations
  5. // Return: CreateTableResponse
  6. CreateTable(request *CreateTableRequest) (*CreateTableResponse, error)

Note: After a table is created, it takes several seconds to load the table. Do not perform any operations during this time.

Example

Create a table with two primary key columns and a reserved read/write throughput of (0,0).

  1. //Create a schema for the primary key columns, including the quantity, names, and types of primary keys
  2. //The first primary key column, the sharding column, is of integer type and named pk0
  3. //The second primary key column is of integer type and named pk1
  4. tableMeta := new(tablestore.TableMeta)
  5. tableMeta.TableName = tableName
  6. tableMeta.AddPrimaryKeyColumn("pk0", tablestore.PrimaryKeyType_INTEGER)
  7. tableMeta.AddPrimaryKeyColumn("pk1", tablestore.PrimaryKeyType_STRING)
  8. tableOption := new(tablestore.TableOption)
  9. tableOption.TimeToAlive = -1
  10. tableOption.MaxVersion = 3
  11. reservedThroughput := new(tablestore.ReservedThroughput)
  12. reservedThroughput.Readcap = 0
  13. reservedThroughput.Writecap = 0
  14. createtableRequest.TableMeta = tableMeta
  15. createtableRequest.TableOption = tableOption
  16. createtableRequest.ReservedThroughput = reservedThroughput
  17. response, err = client.CreateTable(createtableRequest)
  18. if (err != nil) {
  19. fmt.Println("Failed to create table with error:", err)
  20. } else {
  21. fmt.Println("Create table finished")
  22. }

Note: Obtain the full sample codes at createTable@GitHub.

ListTable

Obtains the names of all tables under the current instance.

API

  1. // List all tables. If the operation succeeds, names of all tables are returned
  2. ListTable() (*ListTableResponse, error)

Example

Obtain the names of all tables under an instance.

  1. tables, err := client.ListTable()
  2. if err != nil {
  3. fmt.Println("Failed to list table")
  4. } else {
  5. fmt.Println("List table result is")
  6. for _, table := range (tables.TableNames) {
  7. fmt.Println("TableName: ", table)
  8. }
  9. }

Note: Obtain the full sample codes at listTable@GitHub.

UpdateTable

Updates the reserved read or write throughput value of the specified table.

API

  1. // Update TableOptions and ReservedThroughput of a table
  2. UpdateTable(request *UpdateTableRequest) (*UpdateTableResponse, error)

Example

Set maxVersions of a table to 5.

  1. updateTableReq := new(tablestore.UpdateTableRequest)
  2. updateTableReq.TableName = tableName
  3. updateTableReq.TableOption = new(tablestore.TableOption)
  4. updateTableReq.TableOption.TimeToAlive = -1
  5. updateTableReq.TableOption.MaxVersion = 5
  6. _, err := client.UpdateTable(updateTableReq)
  7. if (err != nil) {
  8. fmt.Println("failed to update table with error:", err)
  9. } else {
  10. fmt.Println("update finished")
  11. }

Note: Obtain the full sample codes at updateTable@GitHub.

DescribeTable

Queries the structure information and the reserved read/write throughput value of the specified table.

API

  1. // Query the description of a table using the table name
  2. DescribeTable(request *DescribeTableRequest) (*DescribeTableResponse, error)

Example

  1. describeTableReq := new(tablestore.DescribeTableRequest)
  2. describeTableReq.TableName = tableName
  3. describ, err := client.DescribeTable(describeTableReq)
  4. if err != nil {
  5. fmt.Println("failed to update table with error:", err)
  6. } else {
  7. fmt.Println("DescribeTableSample finished. Table meta:", describ.TableOption.MaxVersion, describ.TableOption.TimeToAlive)
  8. }

Note: Obtain the full sample codes at describeTable@GitHub.

DeleteTable

Deletes the specified table under an instance.

API

DeleteTable(request DeleteTableRequest) (DeleteTableResponse, error)

Example

Delete a table.

  1. deleteReq := new(tablestore.DeleteTableRequest)
  2. deleteReq.TableName = tableName
  3. _, err := client.DeleteTable(deleteReq)
  4. if (err != nil) {
  5. fmt.Println("Failed to delete table with error:", err)
  6. } else {
  7. fmt.Println("Delete table finished")
  8. }

Note: Obtain the full sample codes at deleteTable@GitHub.

Thank you! We've received your feedback.