Atomic counters allow you to implement an atomic counter on a column. This feature provides statistic data for online applications such as the number of page views (PVs) on various topics.

Note Tablestore SDK for Python V5.1.0 and later support the atomic counter feature.

Prerequisites

  • The OTSClient instance is initialized. For more information, see Initialization.
  • A data table is created. Data is written to the table.

Limits

  • You can implement atomic counters only on INTEGER columns.
  • By default, if a column that is specified as an atomic counter does not exist, the value of the column is 0 before you write data. 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 increased value of an atomic counter is returned.
  • You cannot specify a column as an atomic counter and update the column in a single request. If Attribute Column A is set to an atomic counter, you cannot perform other operations such as overwrite and delete operations on the attribute column A.
  • You can perform multiple update operations on the same row by using 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 is increased. You cannot increase the value of a specified version of an atomic counter. After you update a row, a new version of data is inserted to the atomic counter in the row.

API operations

The API operations to implement the atomic counter feature are added to the updateRow operation. The following table lists these operations.
Operation Description
update_of_attribute_columns Increases or decreases a value by a number. The update type is INCREMENT.
return_content Specifies the column name and return type on which operations are performed to implement atomic counter. The updated value in the column on which operations are performed to implement atomic counter must be returned.

Parameters

Parameter Description
table_name The name of the table.
column_name The name of the column on which operations are performed to implement atomic counter. Columns whose valid values are integers are supported to implement the atomic counter feature.
value The value to update the columns on which operations are performed to implement atomic counter.

Examples

The following code provides an example on how to call the updateRow operation to update the columns whose valid values are integers when you write a row of data. Set the type to INCREMENT for the attribute column:
def increment_by_update_row(client):
    primary_key = [('pk0', 1)]
    # The type of the attribute column is INCREMENT. 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)