Update a row
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) |
|
The name of the table. |
|
row (required) |
|
The primary key and attribute column changes. |
|
condition (required) |
|
The update condition. For more information, see Use conditional updates. To skip row and column value checks, specify |
|
return_type (optional) |
|
The return type. |
|
transaction_id (optional) |
|
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) |
|
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) |
|
The attribute column changes. |
Response
update_row returns the following values.
|
Field |
Type |
Description |
|
consumed |
|
The read and write CUs consumed by the operation. |
|
return_row |
|
The returned row. If |
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),
)