Perform batch write operations to put, update, and delete data across multiple Tablestore tables in a single request by using the Python SDK.
Usage notes
-
If any operation in the request contains a parameter error, the server throws an exception and none of the operations are executed.
-
A single batch write request supports up to 200 rows, and the total data size of all rows cannot exceed 4 MB.
-
When you call the add method multiple times for the same table in a single request, only the last call takes effect.
Prerequisites
Method
def batch_write_row(self, request)
Sample code
The following sample code inserts a row into the test_table table by using a batch write operation.
try:
# Construct the request.
request = BatchWriteRowRequest()
put_row_items = []
# Construct a PutRowItem.
put_pk = [('id', 'row1')]
put_columns = []
put_row = Row(put_pk, put_columns)
# You must specify a condition for the write operation. RowExistenceExpectation.IGNORE means no row existence check is performed.
put_row_item = PutRowItem(put_row, Condition(RowExistenceExpectation.IGNORE))
put_row_items.append(put_row_item)
request.add(TableInBatchWriteRowItem('test_table', put_row_items))
# Call the batch_write_row method to perform the batch operation.
response = client.batch_write_row(request)
# Process the response.
# put row
for table_name,put_rows in response.table_of_put.items():
for item in put_rows:
if not item.is_ok:
print('TableName: %s. Failed to put row, Error message: %s' % (table_name, item.error_message))
# update row
for table_name, update_rows in response.table_of_update.items():
for item in update_rows:
if not item.is_ok:
print('TableName: %s. Failed to update row, Error message: %s.' % (table_name, item.error_message))
# delete row
for table_name, delete_rows in response.table_of_delete.items():
for item in delete_rows:
if not item.is_ok:
print('TableName: %s. Failed to delete row, Error message: %s.' % (table_name, item.error_message))
except Exception as e:
print('Batch write row failed with error: %s' % e)
The following examples demonstrate different types of data operations.
-
PutRowItem: Put row data.
put_row_items = [] # Construct a PutRowItem. put_pk = [('id', 'row1')] put_columns = [] put_row = Row(put_pk, put_columns) # You must specify a condition for the write operation. RowExistenceExpectation.IGNORE means no row existence check is performed. put_row_item = PutRowItem(put_row, Condition(RowExistenceExpectation.IGNORE)) put_row_items.append(put_row_item) request.add(TableInBatchWriteRowItem('test_table', put_row_items))Add attribute columns when you put row data.
# Attribute columns put_columns = [('col1','val1')] # Attribute column with a custom timestamp put_columns = [('col1','val1', int(time.time() * 1000))] -
UpdateRowItem: Update row data. Use this item to modify attribute column values, add attribute columns, delete a specific attribute column version, or delete an entire attribute column.
update_row_items = [] # Construct an UpdateRowItem. update_pk = [('id', 'row1')] update_columns = { 'PUT': [('col1', 'changed_val1')] } update_row = Row(update_pk, update_columns) # You must specify a condition for the update operation. RowExistenceExpectation.IGNORE means no row existence check is performed. update_row_item = UpdateRowItem(update_row, Condition(RowExistenceExpectation.IGNORE)) update_row_items.append(update_row_item) request.add(TableInBatchWriteRowItem('test_table', update_row_items))Add or delete attribute columns when you update row data.
# Add attribute columns. update_columns = { 'PUT': [('col3', 'val3')] } # Add an attribute column with a custom timestamp. update_columns = { 'PUT': [('col4', 'val4', int(time.time() * 1000))] } # Delete attribute columns. update_columns = { 'DELETE_ALL': ['col2'] } -
DeleteRowItem: Delete row data.
delete_row_items = [] # Construct a DeleteRowItem. delete_pk = [('id', 'row1')] delete_row = Row(delete_pk, []) # You must specify a condition for the delete operation. RowExistenceExpectation.IGNORE means no row existence check is performed. delete_row_item = DeleteRowItem(delete_row, Condition(RowExistenceExpectation.IGNORE)) delete_row_items.append(delete_row_item) request.add(TableInBatchWriteRowItem('test_table', delete_row_items)) -
Example of performing multiple operations on a single table
request = BatchWriteRowRequest() table_batch_items = TableInBatchWriteRowItem('<TABLE_NAME>', put_row_items) table_batch_items.row_items += delete_row_items request.add(table_batch_items)