Update a row

Updated at:

Use Tablestore SDK for Python to add, modify, or delete attribute columns in a row and optionally specify data versions.

Prerequisites

Install the Tablestore SDK for Python and initialize a client.

Function description

Call update_row to update a row by primary key. The method changes only the specified attribute columns and leaves other attribute columns unchanged.

def update_row(
    self,
    table_name,
    row,
    condition,
    return_type=None,
    transaction_id=None,
)

The following example changes the status attribute column to offline for the row whose primary key is ("device", 1) in example_table.

primary_key = [("partition", "device"), ("id", 1)]
attribute_columns = {"PUT": [("status", "offline")]}
row = Row(primary_key, attribute_columns)
condition = Condition(RowExistenceExpectation.EXPECT_EXIST)

consumed, return_row = client.update_row(
    "example_table",
    row,
    condition,
)
print("Write CU: %s" % consumed.write)

Parameters

The update_row method contains the following parameters.

Name

Type

Description

table_name (required)

str

The name of the table.

row (required)

Row

The primary key and attribute column changes.

condition (required)

Condition

The update condition. For more information, see Use conditional updates. To skip row and column value checks, specify Condition(RowExistenceExpectation.IGNORE).

return_type (optional)

ReturnType

The return type. RT_NONE, the default value, returns no data. RT_PK returns the primary key.

transaction_id (optional)

str

The local transaction ID. Specify this parameter only for updates in a local transaction. For more information, see Use local transactions.

Row data

The row parameter is of the Row type and contains the following parameters.

Name

Type

Description

primary_key (required)

List[Tuple]

The primary key. Specify all primary key columns. Their names, order, number, and types must match the primary key schema of the table.

attribute_columns (required)

Dict[str, List]

The attribute column changes. PUT adds or modifies attribute columns. DELETE deletes specified versions. DELETE_ALL deletes all versions of attribute columns. INCREMENT performs atomic counter operations on integer attribute columns.

Response

update_row returns the following values.

Field

Type

Description

consumed

CapacityUnit

The read and write CUs consumed by the operation.

return_row

Row

The returned row. If return_type is RT_PK, the primary key is returned. Otherwise, this value is None.

Examples

Write an attribute column with a specified version

The third element of an attribute column tuple in a PUT operation specifies the data version as a timestamp in milliseconds.

timestamp = int(time.time() * 1000)
primary_key = [("partition", "device"), ("id", 1)]
attribute_columns = {
    "PUT": [("status", "online", timestamp)],
}
row = Row(primary_key, attribute_columns)

client.update_row(
    "example_table",
    row,
    Condition(RowExistenceExpectation.EXPECT_EXIST),
)

Delete a specified version of an attribute column

The following example deletes the version identified by timestamp from the status attribute column and retains other versions.

primary_key = [("partition", "device"), ("id", 1)]
attribute_columns = {
    "DELETE": [("status", None, timestamp)],
}
row = Row(primary_key, attribute_columns)

client.update_row(
    "example_table",
    row,
    Condition(RowExistenceExpectation.EXPECT_EXIST),
)

Delete an entire attribute column

The following example deletes all versions of the status attribute column.

primary_key = [("partition", "device"), ("id", 1)]
attribute_columns = {"DELETE_ALL": ["status"]}
row = Row(primary_key, attribute_columns)

client.update_row(
    "example_table",
    row,
    Condition(RowExistenceExpectation.EXPECT_EXIST),
)