Table-level operations

Last Updated: Nov 20, 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. Description: This operation creates a table based on the entered table information.
  2. ``table_meta`` is an instance of the ``tablestore.metadata.TableMeta`` class. It contains the table name and primary key schema.
  3. For more information, see the documentation for the ``TableMeta`` class. After a table is created, it generally takes about 1 minute to load the partitions. After that, you can perform table operations.
  4. ``table_options`` is an instance of the ``tablestore.metadata.TableOptions`` class. It contains time_to_live, max_version, and max_time_deviation.
  5. ``reserved_throughput`` is an instance of the ``tablestore.metadata.ReservedThroughput`` class. It indicates the reserved read/write throughput.
  6. Return: Null.
  7. 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. Wait until the table is loaded.

Example

Create a table with two primary key columns, TTL of one year (that is, 60 * 60 * 24 * 365 = 31536000 seconds), MaxVersions of 3, MaxTimeDeviation of one day (that is, 86400 seconds), 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 integer type. You can also use STRING or BINARY
  4. schema_of_primary_key = [('pk0', 'INTEGER'), ('pk1', 'INTEGER')]
  5. # Create a tableMeta class based on the table name and primary key columns
  6. table_meta = TableMeta('SampleTable', schema_of_primary_key)
  7. # Create TableOptions. Set TTL to 31536000 seconds (The data will be cleared automatically when expired), MaxVersions to 3, and MaxTimeDeviation to one day.
  8. table_options = TableOptions(31536000, 3, 86400)
  9. # Set the reserved read throughput and reserved write throughput to 0
  10. reserved_throughput = ReservedThroughput(CapacityUnit(0, 0))
  11. # Call the create_table API of the client. The execution is successful if no exception is thrown; otherwise, the execution fails
  12. try:
  13. ots_client.create_table(table_meta, table_options, reserved_throughput)
  14. print "create table succeeded"
  15. # Handle exceptions
  16. except Exception:
  17. print "create table failed."

For code details, see 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, see listTable@GitHub.

UpdateTable

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

API

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

Example

Update the Max Versions to 5.

  1. # Update the reserved read throughput to 0 and the reserved write throughput to 0
  2. reserved_throughput = ReservedThroughput(CapacityUnit(0, 0))
  3. # Create TableOptions. Set TTL to 31536000 seconds (The data will be cleared automatically when expired), MaxVersions to 3, and MaxTimeDeviation to one day.
  4. table_options = TableOptions(31536000, 5, 86400)
  5. try:
  6. # Call the API to update the reserved read/write throughput of the table
  7. ots_client.update_table('SampleTable', reserved_throughput)
  8. # The execution is successful if no exception is thrown
  9. print "update table succeeded"
  10. except Exception:
  11. # The call fails if an exception is thrown, and an error message is printed
  12. print "update table failed"

For code details, see 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, see 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, see DeleteTable@GitHub.

Thank you! We've received your feedback.