Table-level operations

Last Updated: May 19, 2017

The Table Store SDK provides the following APIs for manipulating tables:

  • CreateTable
  • ListTable
  • UpdateTable
  • DescribeTable
  • DeleteTable

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, and each primary key column has a name and a type.

API

  1. """
  2. Description: This operation creates a table based on the entered table information.
  3. ``table_meta`` is an instance of the ``ots.metadata.TableMeta`` class. It contains the table name and primary key schema.
  4. For more information, refer to the documentation for the ``TableMeta`` class. After a table is created, it generally takes about 1 minute to load the partitions.
  5. After that, you can perform table operations.
  6. ``reserved_throughput`` is an instance of the ``ots.metadata.ReservedThroughput`` class. This indicates the reserved read/write throughput.
  7. Return: Null.
  8. """
  9. def create_table(self, table_meta, reserved_throughput):

Note: After a table is created in Table Store, it takes several seconds to load the table. You need to wait until the table is loaded.

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 of the integer type
  3. # The second primary key column is named pk1 and of the string type
  4. schema_of_primary_key = [('pk0', 'INTEGER'), ('pk1', 'STRING')]
  5. # Create a tableMeta class based on the table name and primary key columns
  6. table_meta = TableMeta('SampleTable', schema_of_primary_key)
  7. # Set the reserved read throughput and reserved write throughput to 0
  8. reserved_throughput = ReservedThroughput(CapacityUnit(0, 0))
  9. # Call the create_table API of the client. The execution is successful if no exception is thrown; otherwise, the execution fails
  10. try:
  11. ots_client.create_table(table_meta, reserved_throughput)
  12. print "create table succeeded"
  13. # Handle exceptions
  14. except Exception:
  15. print "create table failed."

For code details, refer to CreateTable@GitHub.

ListTable

Obtains the names of all tables under the current instance.

API

  1. """
  2. Description: This operation lists all table names.
  3. Return: Table name list.
  4. ``table_list`` indicates the table name list; type: tuple. For example: ('MyTable1', 'MyTable2').
  5. """
  6. def list_table(self):

Example

Obtain the names of all tables under an instance.

  1. try:
  2. list_response = ots_client.list_table()
  3. print 'table list:'
  4. for table_name in list_response:
  5. print table_name
  6. print "list table succeeded"
  7. except Exception:
  8. print "list table failed."

For code details, refer to listTable@GitHub.

UpdateTable

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

API

  1. """
  2. Description: This operation updates table attributes. At present, this function can only modify reserved read/write throughput.
  3. ``table_name`` is the name of the table.
  4. ``reserved_throughput`` is an instance of the ``ots2.metadata.ReservedThroughput`` class. This indicates the reserved read/write throughput.
  5. Return: The last time that the reserved throughput was raised or lowered for this table and the number of times it was lowered today.
  6. ``update_table_response`` indicates the update results. This is an instance of the ots2.metadata.UpdateTableResponse class.
  7. """
  8. def update_table(self, table_name, reserved_throughput):

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. reserved_throughput = ReservedThroughput(CapacityUnit(1, 2))
  3. try:
  4. # Call the API to update the reserved read/write throughput of the table
  5. ots_client.update_table('SampleTable', reserved_throughput)
  6. # The execution is successful if no exception is thrown
  7. print "update table succeeded"
  8. except Exception:
  9. # The call fails if an exception is thrown, and an error message is printed
  10. print "update table failed"
  11. }

For code details, refer to UpdateTable@GitHub.

DescribeTable

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

API

  1. """
  2. Description: This operation gets the table description.
  3. ``table_name`` is the name of the table.
  4. Return: Table description.
  5. ``describe_table_response`` indicates the table description. This is an instance of the ots2.metadata.DescribeTableResponse class.
  6. """
  7. def describe_table(self, table_name):

Example

Obtain the descriptive information of a table.

  1. try:
  2. describe_response = ots_client.describe_table('myTable')
  3. # If no exception is thrown, execution is successful and the following table information is printed
  4. print "describe table succeeded."
  5. print 'read cu:%s' % update_response.reserved_throughput_details.capacity_unit.read
  6. print 'write cu:%s' % update_response.reserved_throughput_details.capacity_unit.write
  7. except Exception:
  8. # If an exception is thrown, the operation fails and the exception is processed
  9. print "describe table failed."

For code details, refer to DescribeTable@GitHub.

DeleteTable

Deletes the specified table under an instance.

API

  1. """
  2. Description: This operation deletes the table corresponding to the specified table name.
  3. ``table_name`` is the name of the table.
  4. Return: Null.
  5. """
  6. def delete_table(self, table_name):

Example

Delete a table.

  1. try:
  2. # Call the API to delete the table SampleTable
  3. ots_client.delete_table('SampleTable')
  4. # If no exception is thrown, the operation is successful
  5. print "delete table succeeded"
  6. # If an exception is thrown, the operation fails and the exception is processed
  7. except Exception:
  8. print "delete table failed"

For code details, refer to DeleteTable@GitHub.

Thank you! We've received your feedback.