Atomic counter provides real-time statistics for online applications, such as the real-time page views (PVs) of a post.

Note For more information about atomic counters, see Atomic counters.

Operations

The following operations have been added to the RowUpdateChange class to operate an atomic counter:
  • RowUpdateChange increment(Column column) is used to increment or decrement an attribute value by a number.
  • ReturnColumnNames is used to specify the column on which the atomic counter operation is performed.
  • ReturnType = ReturnType.RT_AFTER_MODIFY is used to specify a flag to indicate that the updated value of the atomic counter must be returned.

Example

The following code provides an example on how to use the RowUpdateChange operation to update the columns whose valid values are integers when writing a row of data:
public static void Increment(int incrementValue)
{
    Console.WriteLine("Start set increment column...") ;
    OTSClient otsClient = Config.GetClient();

    // Specify the primary keys of the row. These primary keys must be consistent with the primary keys specified in TableMeta during table creation.
    PrimaryKey primaryKey = new PrimaryKey
    {
        { Pk1, new ColumnValue(0) },
        { Pk2, new ColumnValue("abc") }
    };
    RowUpdateChange rowUpdateChange = new RowUpdateChange(TableName, primaryKey);
    rowUpdateChange.ReturnType = ReturnType.RT_AFTER_MODIFY;
    rowUpdateChange.ReturnColumnNames = new List<string>() { IncrementCol};
    // Set an atomic auto-increment column. The column value starts from 0 and increments by the number specified by the incrementValue parameter .
    rowUpdateChange.Increment(new Column(IncrementCol, new ColumnValue(incrementValue)));

    UpdateRowRequest updateRowRequest = new UpdateRowRequest(rowUpdateChange);

    var response = otsClient.UpdateRow(updateRowRequest);
    Console.WriteLine("set Increment column succeed,Increment result:" + response.Row.GetColumns()[0].Value);
}