All Products
Search
Document Center

Tablestore:Use the atomic counter feature

Last Updated:Jul 01, 2024

If you want to use a counter for your online application, you can use the atomic counter feature. To use the atomic counter feature, specify a column as an atomic counter and perform atomic counter operations on the column.

Note

Tablestore SDK for Python V5.1.0 or later supports the atomic counter feature.

Prerequisites

  • An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.

  • A data table is created and data is written to the data table.

Usage notes

  • You can implement atomic counters only on INTEGER columns.

  • If a column that is specified as an atomic counter does not exist before you write data, the default value of the column is 0. If a column that is specified as an atomic counter is not an INTEGER column, an OTSParameterInvalid error occurs.

  • You can update an atomic counter by using a positive or negative number, but you must avoid an integer overflow. If an integer overflow occurs, an OTSParameterInvalid error is returned.

  • By default, the value of an atomic counter is not returned in the response to an update row request. You can specify that the updated value of an atomic counter is returned.

  • You cannot specify a column as an atomic counter and update the column in a single update request. For example, if you set Column A to an atomic counter, you cannot perform other operations such as overwrite and delete operations on the column at the same time.

  • You can perform multiple update operations on the same row by sending a BatchWriteRow request. However, if you perform an atomic counter operation on a row, you can perform only one update operation on the row in a BatchWriteRow request.

  • Only the value of the latest version of an atomic counter can be updated. You cannot update the value of a specified version of an atomic counter. After an update operation is complete, a new version of data is inserted into the atomic counter in the row.

API operation

The atomic counter-related operation is added to the updateRow operation. The following table describes the atomic counter-related operation.

Operation

Description

update_of_attribute_columns

Specifies INCREMENT as the update type to increase or decrease a column value by a specific integer.

Parameters

Parameter

Description

table_name

The name of the data table.

column_name

The name of the column on which you want to perform atomic counter operations. You can specify a column only of the INTEGER type.

value

The increment or decrement in the column value.

Examples

The following sample code provides an example on how to use INCREMENT as the update type to update the value of an attribute column of the INTEGER type when you call the updateRow operation to update data:

def increment_by_update_row(client):
    # Specify the name of the data table. 
    table_name ='<TABLE_NAME>'
    primary_key = [('pk0', 1)]
    # Use INCREMENT as the update type. In this example, specify that the value of the price column is increased by 6. 
    update_of_attribute_columns = {
        'INCREMENT': [('price', 6)]
    }
    row = Row(primary_key, update_of_attribute_columns)
    consumed, return_row = client.update_row(table_name, row, None)
    print ('Update succeed, consume %s write cu.' % consumed.write)