Use Tablestore SDK for Python to add conditions to row put, update, or delete operations so that an operation runs only if the target row meets the conditions.
Prerequisites
Install the Tablestore SDK for Python and initialize a client.
Function description
Use Condition to specify a row existence condition and an attribute column value condition. Pass the condition to put_row, update_row, delete_row, or the corresponding batch row operation. If the condition is not met, the server returns an error and does not modify the row.
Condition(row_existence_expectation, column_condition=None)
The following example updates the status attribute column only if the target row exists.
primary_key = [("partition", "device"), ("id", 1)]
row = Row(primary_key, {"PUT": [("status", "online")]})
condition = Condition(RowExistenceExpectation.EXPECT_EXIST)
consumed, return_row = client.update_row(
"example_table",
row,
condition,
)
Parameters
Update condition
Condition contains the following parameters.
|
Name |
Type |
Description |
|
row_existence_expectation (required) |
|
The row existence condition. |
|
column_condition (optional) |
|
The attribute column value condition. |
Single-column value condition
Call SingleColumnCondition(column_name, column_value, comparator, pass_if_missing=True, latest_version_only=True) to create a single-column value condition.
|
Name |
Type |
Description |
|
column_name (required) |
|
The name of the attribute column to evaluate. |
|
column_value (required) |
|
The target value. Its type must match the attribute column value type. |
|
comparator (required) |
|
The comparison operator. Valid values are |
|
pass_if_missing (optional) |
|
Specifies whether the condition is met if the target attribute column is missing. Default value: |
|
latest_version_only (optional) |
|
Specifies whether to evaluate only the latest version. Default value: |
Single-column regular expression condition
Call SingleColumnRegexCondition(column_name, comparator, column_value=None, regex_rule=None, latest_version_only=True) to create a single-column regular expression condition. Only string attribute columns support regular expression conditions.
|
Name |
Type |
Description |
|
column_name (required) |
|
The name of the string attribute column to evaluate. |
|
comparator (required) |
|
The comparison operator. Valid values are |
|
column_value (optional) |
|
The target value. Omit this parameter if |
|
regex_rule (optional) |
|
The substring extraction and type conversion rule. If this parameter is omitted, the attribute column value is compared directly. |
|
latest_version_only (optional) |
|
Specifies whether to evaluate only the latest version. Default value: |
If the target attribute column is missing, a single-column regular expression condition is never met. pass_if_missing cannot be configured separately for this condition.
Regular expression rule
The regex_rule parameter is of the RegexRule type and contains the following parameters.
|
Name |
Type |
Description |
|
regex_input (required) |
|
The regular expression, up to 256 bytes. Perl-compatible expressions and single-byte characters are supported. Chinese characters are not supported. If the expression contains capturing groups, the first group is extracted. Otherwise, the entire match is extracted. |
|
cast_type (required) |
|
The type to which the extracted value is converted. Valid values are |
Composite column value condition
Call CompositeColumnCondition(combinator) to create a composite column value condition, and then call add_sub_condition to add conditions.
|
Name |
Type |
Description |
|
combinator (required) |
|
The logical operator. Valid values are |
|
sub_conditions (required) |
|
The conditions to combine. Single-column value conditions, single-column regular expression conditions, and nested composite conditions are supported, with a maximum of 32 conditions. |
Examples
Use a single-column value condition
The following example updates data only if the target row exists and the latest value of the status attribute column is pending. The condition is not met if the column is missing.
column_condition = SingleColumnCondition(
"status",
"pending",
ComparatorType.EQUAL,
pass_if_missing=False,
)
condition = Condition(
RowExistenceExpectation.EXPECT_EXIST,
column_condition,
)
client.update_row("example_table", row, condition)
Combine column value conditions
The following example constructs the condition (status == "pending" AND revision == 1) OR priority > 5.
status_and_revision = CompositeColumnCondition(LogicalOperator.AND)
status_and_revision.add_sub_condition(
SingleColumnCondition("status", "pending", ComparatorType.EQUAL)
)
status_and_revision.add_sub_condition(
SingleColumnCondition("revision", 1, ComparatorType.EQUAL)
)
column_condition = CompositeColumnCondition(LogicalOperator.OR)
column_condition.add_sub_condition(status_and_revision)
column_condition.add_sub_condition(
SingleColumnCondition("priority", 5, ComparatorType.GREATER_THAN)
)
condition = Condition(RowExistenceExpectation.EXPECT_EXIST, column_condition)
Implement optimistic locking with CAS
The following example reads the current revision value and uses it as an update condition. The revision is incremented only if another request has not modified the data.
primary_key = [("partition", "device"), ("id", 1)]
consumed, current_row, next_token = client.get_row(
"example_table",
primary_key,
columns_to_get=["revision"],
)
old_revision = current_row.attribute_columns[0][1]
row = Row(primary_key, {"PUT": [("revision", old_revision + 1)]})
column_condition = SingleColumnCondition(
"revision",
old_revision,
ComparatorType.EQUAL,
pass_if_missing=False,
latest_version_only=True,
)
condition = Condition(RowExistenceExpectation.EXPECT_EXIST, column_condition)
client.update_row("example_table", row, condition)