Batch row operations

Updated at:

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())
Note

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)

BatchWriteRowRequest

The batch row operation request. Call add to add one TableInBatchWriteRowItem for each table. Configurations are stored by table name. If the same table is added more than once, the latest configuration replaces the previous one.

Table operation configurations

Each element in request.items[] is of the TableInBatchWriteRowItem type and contains the following parameters.

Name

Type

Description

table_name (required)

str

The name of the table.

row_items (required)

List[RowItem]

The row operations. Supported types are PutRowItem, UpdateRowItem, and DeleteRowItem.

Row operations

Each element in row_items[] contains the following common parameters.

Name

Type

Description

row (required)

Row

The row data. For PutRowItem, specify the primary key and attribute columns to write. For UpdateRowItem, specify the primary key and attribute column changes. For DeleteRowItem, specify only the primary key.

condition (required)

Condition

The operation 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.

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

is_all_succeed()

bool

Indicates whether all row operations succeeded.

get_succeed_of_put() / get_failed_of_put()

List[BatchWriteRowResponseItem]

The successful or failed put results.

get_succeed_of_update() / get_failed_of_update()

List[BatchWriteRowResponseItem]

The successful or failed update results.

get_succeed_of_delete() / get_failed_of_delete()

List[BatchWriteRowResponseItem]

The successful or failed delete results.

get_*_by_table(table_name)

List[BatchWriteRowResponseItem]

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)