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:
  • IncrementColumn(columnName string, value int64) is used to increment or decrement an attribute value by a number.
  • AppendIncrementColumnToReturn(name string) is used to specify the column on which the atomic counter operation is performed.
  • SetReturnIncrementValue() 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 columns whose valid values are integers when writing a row of data:
func UpdateRowWithIncrement(client *tablestore.TableStoreClient, tableName string) {
    fmt.Println("begin to put row")
    updateRowRequest = new(tablestore.UpdateRowRequest)
    updateRowChange = new(tablestore.UpdateRowChange)
    updateRowChange.TableName = tableName
    updatePk = new(tablestore.PrimaryKey)
    updatePk.AddPrimaryKeyColumn("pk1", "pk1increment")
    updatePk.AddPrimaryKeyColumn("pk2", int64(2))
    updatePk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
    updateRowChange.PrimaryKey = updatePk

    updateRowChange.IncrementColumn("col2", int64(30))
    updateRowChange.SetReturnIncrementValue()
    updateRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
    updateRowChange.AppendIncrementColumnToReturn("col2")
    updateRowRequest.UpdateRowChange = updateRowChange

    resp, err := client.UpdateRow(updateRowRequest)
    if err ! = nil {
        fmt.Println("update failed with error:", err)
        return
    } else {
        fmt.Println("update row finished")
        fmt.Println(resp)
        fmt.Println(len(resp.Columns))
        fmt.Println(resp.Columns[0].ColumnName)
        fmt.Println(resp.Columns[0].Value)
        fmt.Println(resp.Columns[0].Timestamp)
    }
}