If you want to implement a counter for your online application, you can use the atomic counter feature. To use this feature, set a column as an atomic counter and perform atomic counter operations on the column.
Prerequisites
-
The client has been initialized. For more information, see Initialize the Tablestore client.
-
A data table has been created and data has been written to it. For more information, see Create a 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.
Syntax
/**
* Update a row of data.
* @api
* @param [] $request The request parameters.
* @return [] The response.
* @throws OTSClientException The exception that is thrown if a parameter error occurs or the Tablestore server returns a verification error.
* @throws OTSServerException The exception that is thrown if the Tablestore server returns an error.
*/
public function updateRow(array $request);
Call updateRow to perform atomic counter operations. The following table lists the supported operations.
|
Operation |
Description |
|
update_of_attribute_columns |
Set the update type to INCREMENT to increment or decrement a column value by a specific integer. Configuration format:
|
|
return_content |
Returns the updated column value. Specify the column name and set return_type to
|
Parameters
|
Parameter |
Description |
|
table_name |
The name of the table. |
|
column_name |
The name of the column on which to perform the atomic counter operation. Only INTEGER columns are supported. |
|
value |
The amount by which to increment or decrement the column value. |
|
return_content |
The column information to return after the atomic counter operation. Contains the following parameters:
|
Example
The following example increments an INTEGER column value when writing data and returns the updated value.
$request = array (
// Specify the name of the table.
'table_name' => 'MyTable',
// Set the row existence condition to RowExistenceExpectationConst::CONST_IGNORE, which specifies that the existence of rows is not checked.
'condition' => RowExistenceExpectationConst::CONST_IGNORE,
// Specify the primary key. The number and types of primary key columns that you specify must be the same as the actual number and types of primary key columns in the table.
'primary_key' => array (
// The name of the first primary key column is PK0, the data type is INTEGER, and the column value is 123.
array('PK0', 123),
// The name of the second primary key column is PK1, the data type is STRING, and the column value is 'inc'.
array('PK1', 'inc')
),
// The attribute column whose value is to be updated. Three operation types are supported: PUT, DELETE, and DELETE_ALL.
'update_of_attribute_columns'=> array(
'INCREMENT' => array (
// Specify the column on which the atomic counter operation is to be performed. The column name is attr0, the increment in the column value is 1, and the data type is INTEGER.
array('attr0', 1, ColumnTypeConst::CONST_INTEGER),
),
'PUT' => array(
array('attr1', 1, ColumnTypeConst::CONST_INTEGER),
)
),
// Return the column information after the atomic counter operation is performed.
'return_content' => array(
'return_type' => ReturnTypeConst::CONST_AFTER_MODIFY,
'return_column_names' => array('attr0')
)
);
$response = $otsClient->updateRow ($request);
print json_encode ($response);