Batch row operations
Use Tablestore SDK for Python to write, update, and delete rows in one or more tables in a single request.
Prerequisites
Install the Tablestore SDK for Python and initialize a client.
Function description
Call batch_write_row to perform batch row operations. The result of each row operation is returned independently.
def batch_write_row(self, request)
The following example writes two rows to example_table.
condition = Condition(RowExistenceExpectation.IGNORE)
row_items = [
PutRowItem(
Row([("partition", "device"), ("id", 1)], [("status", "online")]),
condition,
),
PutRowItem(
Row([("partition", "device"), ("id", 2)], [("status", "offline")]),
condition,
),
]
request = BatchWriteRowRequest()
request.add(TableInBatchWriteRowItem("example_table", row_items))
response = client.batch_write_row(request)
print(response.is_all_succeed())
A single batch request supports up to 200 rows, and the total size of all rows cannot exceed 4 MB. If the server detects an invalid parameter in any operation, it returns a parameter error and does not execute any operation in the batch.
Parameters
The batch_write_row method contains the following parameter.
|
Name |
Type |
Description |
|
request (required) |
|
The batch row operation request. Call |
Table operation configurations
Each element in request.items[] is of the TableInBatchWriteRowItem type and contains the following parameters.
|
Name |
Type |
Description |
|
table_name (required) |
|
The name of the table. |
|
row_items (required) |
|
The row operations. Supported types are |
Row operations
Each element in row_items[] contains the following common parameters.
|
Name |
Type |
Description |
|
row (required) |
|
The row data. For |
|
condition (required) |
|
The operation condition. For more information, see Use conditional updates. To skip row and column value checks, specify |
|
return_type (optional) |
|
The return type. |
Local transactions
Call request.set_transaction_id(transaction_id) to include a local transaction ID in the batch request. In a local transaction, the request can contain rows only from the table associated with the transaction, and all rows must have the partition key value used to create the transaction. For more information, see Use local transactions.
Response
batch_write_row returns a BatchWriteRowResponse. Use the following methods to obtain results.
|
Method |
Return type |
Description |
|
|
|
Indicates whether all row operations succeeded. |
|
|
|
The successful or failed put results. |
|
|
|
The successful or failed update results. |
|
|
|
The successful or failed delete results. |
|
|
|
The per-row results for the specified table and operation type. |
Each BatchWriteRowResponseItem contains the success status, error code, error message, consumed CUs, and primary key.
Examples
Update multiple rows
The following example updates the status attribute column in two rows.
condition = Condition(RowExistenceExpectation.EXPECT_EXIST)
row_items = [
UpdateRowItem(
Row([("partition", "device"), ("id", 1)], {"PUT": [("status", "online")]}),
condition,
),
UpdateRowItem(
Row([("partition", "device"), ("id", 2)], {"PUT": [("status", "online")]}),
condition,
),
]
request = BatchWriteRowRequest()
request.add(TableInBatchWriteRowItem("example_table", row_items))
response = client.batch_write_row(request)
Delete multiple rows
The following example deletes two rows.
condition = Condition(RowExistenceExpectation.EXPECT_EXIST)
row_items = [
DeleteRowItem(Row([("partition", "device"), ("id", 1)]), condition),
DeleteRowItem(Row([("partition", "device"), ("id", 2)]), condition),
]
request = BatchWriteRowRequest()
request.add(TableInBatchWriteRowItem("example_table", row_items))
response = client.batch_write_row(request)
Mix operation types across tables
The following example writes a row to example_table and updates a row in another_table in the same request.
put_row = Row(
[("partition", "device"), ("id", 3)],
[("status", "online")],
)
update_row = Row(
[("partition", "order"), ("id", 1)],
{"PUT": [("status", "processed")]},
)
request = BatchWriteRowRequest()
request.add(
TableInBatchWriteRowItem(
"example_table",
[PutRowItem(put_row, Condition(RowExistenceExpectation.IGNORE))],
)
)
request.add(
TableInBatchWriteRowItem(
"another_table",
[UpdateRowItem(update_row, Condition(RowExistenceExpectation.EXPECT_EXIST))],
)
)
response = client.batch_write_row(request)