Use the Tablestore SDK for Go to specify conditions for a row write, update, or deletion so that the operation is performed only when the target row meets the conditions.
Prerequisites
Install the Tablestore SDK for Go and initialize the client.
Description
A conditional update can contain a row existence condition, a column condition, or both. Call SetCondition and SetColumnCondition on PutRowChange, UpdateRowChange, or DeleteRowChange. You can also add these row change objects to a BatchWriteRow request. If the conditions are not met, the server returns an error and does not modify data.
A row existence condition checks whether the target row exists. You can ignore row existence, require the row to exist, or require the row not to exist.
A column condition compares one column value or combines multiple column conditions by using logical operators. A single-column condition can also reference a primary key column. Column-to-column and constant-to-constant comparisons are not supported. An update can contain up to 10 column conditions.
The following sample updates the status column only if the target row exists and the value of the score column is 30.
primaryKey := &tablestore.PrimaryKey{}
primaryKey.AddPrimaryKeyColumn("id", "row1")
change := &tablestore.UpdateRowChange{
TableName: "example_table",
PrimaryKey: primaryKey,
}
change.PutColumn("status", "processed")
change.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
change.SetColumnCondition(
tablestore.NewSingleColumnCondition("score", tablestore.CT_EQUAL, int64(30)),
)
_, err := client.UpdateRow(&tablestore.UpdateRowRequest{UpdateRowChange: change})
if err != nil {
log.Fatal(err)
}
Parameters
Row existence condition
Call SetCondition to specify a RowExistenceExpectation value.
|
Name |
Type |
Description |
|
expectation (required) |
|
The row existence condition. Valid values: |
Single-column condition
Call NewSingleColumnCondition(columnName, comparator, value) to create a single-column condition. SingleColumnCondition contains the following parameters.
|
Name |
Type |
Description |
|
ColumnName (required) |
|
The column to evaluate. This can be an attribute column or primary key column. |
|
Comparator (required) |
|
The comparison operator. Valid values: |
|
ColumnValue (required) |
|
The constant value to compare against. |
|
FilterIfMissing (optional) |
|
Specifies whether the condition is not met if the row does not contain the column. Default value: |
|
LatestVersionOnly (optional) |
|
Specifies whether to evaluate only the latest version. Default value: |
Composite condition
Call NewCompositeColumnCondition(lo) to create a composite condition, and call AddFilter to add subconditions. A subcondition can be a single-column condition or a nested composite condition. CompositeColumnValueFilter contains the following parameters.
|
Name |
Type |
Description |
|
Operator (required) |
|
The logical operator. |
|
Filters (required) |
|
The subconditions to combine. |
Examples
Update data by using a composite condition
The following sample performs an update only if score >= 60 and status is pending.
condition := tablestore.NewCompositeColumnCondition(tablestore.LO_AND)
condition.AddFilter(
tablestore.NewSingleColumnCondition("score", tablestore.CT_GREATER_EQUAL, int64(60)),
)
condition.AddFilter(
tablestore.NewSingleColumnCondition("status", tablestore.CT_EQUAL, "pending"),
)
change.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
change.SetColumnCondition(condition)