edit-icon download-icon

Table-level operations

Last Updated: Apr 08, 2018

The Table Store SDK provides the following table-level operation interfaces: 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. /// <summary>
  2. /// Create a table based on table information (including the table name, primary key design, and reserved read/write throughput).
  3. /// </summary>
  4. /// <param name="request">Request parameter</param>
  5. /// <returns>Information returned by CreateTable. The returned instance is null, no specific information.
  6. /// </returns>
  7. public CreateTableResponse CreateTable(CreateTableRequest request);
  8. /// <summary>
  9. /// Asynchronous form of CreateTable.
  10. /// </summary>
  11. public Task<CreateTableResponse> CreateTableAsync(CreateTableRequest request);

Note: After a table is created in Table Store, it takes several seconds to fully 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 (sharding column) is named pk0 and belongs to the integer type.
  3. //The second primary key column is named pk1 and belongs to the string type.
  4. var primaryKeySchema = new PrimaryKeySchema();
  5. primaryKeySchema.Add("pk0", ColumnValueType.Integer);
  6. primaryKeySchema.Add("pk1", ColumnValueType.String);
  7. //Create a tableMeta based on the table name and primary key columns
  8. var tableMeta = new TableMeta("SampleTable", primaryKeySchema);
  9. // Set the reserved read throughput and reserved write throughput to 0
  10. var reservedThroughput = new CapacityUnit(0, 0);
  11. try
  12. {
  13. // Construct the CreateTableRequest object
  14. var request = new CreateTableRequest(tableMeta, reservedThroughput);
  15. // Call the CreateTable interface of the client. The table is created successfully if no exception is thrown. If an exception is thrown, the table fails to be created.
  16. otsClient.CreateTable(request);
  17. Console.WriteLine("Create table succeeded.");
  18. }
  19. // Handle exceptions
  20. catch (Exception ex)
  21. {
  22. Console.WriteLine("Create table failed, exception:{0}", ex.Message);
  23. }

Note: Code details can be obtained at CreateTable@GitHub.

ListTable

Obtains the names of all tables under the current instance.

API

  1. /// <summary>
  2. /// Obtain the names of all tables under the current instance.
  3. /// </summary>
  4. /// <param name="request">Request parameter</param>
  5. /// <returns>Information returned by ListTable and used to obtain the table name list.</returns>
  6. public ListTableResponse ListTable(ListTableRequest request);
  7. /// <summary>
  8. /// Asynchronous form of ListTable.
  9. /// </summary>
  10. public Task<ListTableResponse> ListTableAsync(ListTableRequest request);

Example

Obtain the names of all tables under an instance.

  1. var request = new ListTableRequest();
  2. try
  3. {
  4. var response = otsClient.ListTable(request);
  5. foreach (var tableName in response.TableNames)
  6. {
  7. Console.WriteLine("Table name:{0}", tableName);
  8. }
  9. Console.Writeline("List table succeeded.");
  10. }
  11. catch (Exception ex)
  12. {
  13. Console.WriteLine("List table failed, exception:{0}", ex.Message);
  14. }

UpdateTable

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

API

  1. /// <summary>
  2. /// Update the reserved read or write throughput value of the specified table. The new value takes effect within 1 minute after the throughput is updated successfully.
  3. /// </summary>
  4. /// <param name="request">Request parameter, including the table name and reserved read/write throughput</param>
  5. /// <returns>Contain the updated reserved read/write throughput and other information</returns>
  6. public UpdateTableResponse UpdateTable(UpdateTableRequest request);
  7. /// <summary>
  8. /// Asynchronous form of UpdateTable.
  9. /// </summary>
  10. public Task<UpdateTableResponse> UpdateTableAsync(UpdateTableRequest request);

Example

Update the read CU and write CU of a table to 1 and 2 respectively.

  1. // Update the reserved read throughput to 1 and the reserved write throughput to 2
  2. var reservedThroughput = new CapacityUnit(1, 2);
  3. // Construct the UpdateTableRequest object
  4. var request = new UpdateTableRequest("SampleTable", reservedThroughput);
  5. try
  6. {
  7. // Call the interface to update the reserved read/write throughput of the table
  8. otsClient.UpdateTable(request);
  9. // Execution is successful if no exception is thrown.
  10. Console.Writeline("Update table succeeded.");
  11. }
  12. catch (Exception ex)
  13. {
  14. // Execution fails if an exception is thrown, and an error message is printed.
  15. Console.WriteLine("Update table failed, exception:{0}", ex.Message);
  16. }

Note: Code details can be obtained at UpdateTable@GitHub.

DescribeTable

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

API

  1. /// <summary>
  2. /// Query the structure information and the reserved read/write throughput value of the specified table.
  3. /// </summary>
  4. /// <param name="request">Request parameter, including the table name</param>
  5. /// <returns>Contain the structure information and the reserved read/write throughput value of the specified table.</returns>
  6. public DescribeTableResponse DescribeTable(DescribeTableRequest request);
  7. /// <summary>
  8. /// Asynchronous form of DescribeTable.
  9. /// </summary>
  10. public Task<DescribeTableResponse> DescribeTableAsync(DescribeTableRequest request);

Example

Obtain the descriptive information of a table.

  1. try
  2. {
  3. var request = new DescribeTableRequest("SampleTable");
  4. var response = otsClient.DescribeTable(request);
  5. // Print the descriptive information of the specified table
  6. Console.Writeline("Describe table succeeded.");
  7. Console.WriteLine("LastIncreaseTime: {0}", response.ReservedThroughputDetails.LastIncreaseTime);
  8. Console.WriteLine("LastDecreaseTime: {0}", response.ReservedThroughputDetails.LastDecreaseTime);
  9. Console.WriteLine("NumberOfDecreaseToday: {0}", response.ReservedThroughputDetails.LastIncreaseTime);
  10. Console.WriteLine("ReadCapacity: {0}", response.ReservedThroughputDetails.CapacityUnit.Read);
  11. Console.WriteLine("WriteCapacity: {0}", response.ReservedThroughputDetails.CapacityUnit.Write);
  12. }
  13. catch (Exception ex)
  14. {
  15. // Execution fails if an exception is thrown, and an error message is printed.
  16. Console.WriteLine("Describe table failed, exception:{0}", ex.Message);
  17. }

Note: Code details can be obtained at DescribeTable@GitHub.

DeleteTable

Deletes the specified table under an instance.

API

  1. /// <summary>
  2. /// Delete the table that corresponds to the specified table name.
  3. /// </summary>
  4. /// <param name="request">Request parameter, including the table name</param>
  5. /// <returns>Information returned by DeleteTable. The returned instance is null, no specific information.
  6. /// </returns>
  7. public DeleteTableResponse DeleteTable(DeleteTableRequest request);
  8. /// <summary>
  9. /// Asynchronous form of DeleteTable.
  10. /// </summary>
  11. public Task<DeleteTableResponse> DeleteTableAsync(DeleteTableRequest request);

Example

Delete a table.

  1. var request = new DeleteTableRequest("SampleTable");
  2. try
  3. {
  4. otsClient.DeleteTable(request);
  5. Console.Writeline("Delete table succeeded.");
  6. }
  7. catch (Exception ex)
  8. {
  9. Console.WriteLine("Delete table failed, exception:{0}", ex.Message);
  10. }

Note: Code details can be obtained at DeleteTable@GitHub.

Thank you! We've received your feedback.