All Products
Search
Document Center

Tablestore:Update a table

Last Updated:Apr 10, 2024

You can call the UpdateTable operation to update the attributes of a table, such as the max versions. You can also update the reserved read or write throughput of a table in a high-performance instance.

API operation

"""
Description: You can call the UpdateTable operation to update the attributes of a table, including the reserved read or write throughput of a table. 
table_name: the name of the table. 
table_options: includes the time_to_live, max_version, and max_time_deviation parameters. The table_options parameter is an instance of the tablestore.metadata.TableOptions class. 
reserved_throughput: the reserved read or write throughput. The reserved_throughput parameter is an instance of the ots2.metadata.ReservedThroughput class. 

Response: the time when the reserved read or write throughput was most recently increased or decreased for the table and the number of times the reserved read or write throughput is decreased on the current day. 

update_table_response: the update result. The update_table_response parameter is an instance of the ots2.metadata.UpdateTableResponse class. 
"""
def update_table(self, table_name, table_options, reserved_throughput):

Parameters

For more information, see Create data tables.

Examples

Update the attributes of a table in a capacity instance

The following sample code provides an example on how to update the max versions of a table to 5:

# Create a table_options instance. Set the time to live (TTL) of data in the table to 31536000 seconds. If the retention period of data in the table exceeds the TTL value, the data is automatically deleted. Specify that up to five versions of data in the table can be retained and the maximum difference between the current system time and the specified data version is one day. 
table_options = TableOptions(31536000, 5, 86400)

try:
    # Call the UpdateTable operation to update the reserved read or write throughput of the table. 
    ots_client.update_table('SampleTable', table_options, None)
    # If no exception is thrown, the update is successful. 
    print("update table succeeded")
except Exception:
    # If an exception is thrown, the update fails. Handle the exception. 
    print("update table failed")

For the detailed sample code, visit UpdateTable@GitHub.

Update the attributes of a table in a high-performance instance

The following sample code provides an example on how to update the max versions of a table to 5 and the reserved read and write throughput to 0:

# Set the new reserved read and write throughput to 0. 
reserved_throughput = ReservedThroughput(CapacityUnit(0, 0))

# Create a table_options instance. Set the TTL of data in the table to 31536000 seconds. If the retention period of data in the table exceeds the TTL value, the data is automatically deleted. Specify that up to five versions of data in the table can be retained and the maximum difference between the current system time and the specified data version is one day. 
table_options = TableOptions(31536000, 5, 86400)

try:
    # Call the UpdateTable operation to update the reserved read or write throughput of the table. 
    ots_client.update_table('SampleTable', table_options, reserved_throughput)
    # If no exception is thrown, the update is successful. 
    print("update table succeeded")
except Exception:
    # If an exception is thrown, the update fails. Handle the exception. 
    print("update table failed")