Batch update data
The Tablestore Go SDK BatchWriteRow operation puts, updates, or deletes multiple rows across one or more tables in a single request.
Important notes
-
If any operation in the request has invalid parameters, the server throws a parameter error and does not execute any operation in that request.
-
Each batch update request supports at most 200 rows. The combined size of all rows in the request cannot exceed 4 MB.
Prerequisites
Method and parameters
func (tableStoreClient *TableStoreClient) BatchWriteRow(request *BatchWriteRowRequest) (*BatchWriteRowResponse, error)
Code examples
This example calls BatchWriteRow to insert one row into the test_table table.
func BatchWriteRowSample(client *tablestore.TableStoreClient) {
// Construct the request.
batchWriteRequest := new(tablestore.BatchWriteRowRequest)
// Add a PutRowChange.
putPk := new(tablestore.PrimaryKey)
putPk.AddPrimaryKeyColumn("id", "row1")
putRowChange := new(tablestore.PutRowChange)
putRowChange.TableName = "test_table"
putRowChange.PrimaryKey = putPk
// You must set a condition for the operation. RowExistenceExpectation_IGNORE bypasses the row existence check.
putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
batchWriteRequest.AddRowChange(putRowChange)
// Call the batchWriteRow method to perform the batch operation.
response, err := client.BatchWriteRow(batchWriteRequest)
// Process the response.
if err != nil {
fmt.Println("Batch write row failed with error:", err)
} else {
fmt.Printf("RequestId: %s \n", response.RequestId)
for tableName, rows := range response.TableToRowsResult {
for _, row := range rows {
if !row.IsSucceed {
fmt.Printf("TableName: %s. Failed Rows: %v \n", tableName, row.Error)
}
}
}
}
}
The following examples demonstrate PutRowChange, UpdateRowChange, and DeleteRowChange individually.
-
PutRowChange: Inserts or overwrites a row (put).putPk := new(tablestore.PrimaryKey) putPk.AddPrimaryKeyColumn("id", "row1") putRowChange := new(tablestore.PutRowChange) putRowChange.TableName = "test_table" putRowChange.PrimaryKey = putPk // You must set a condition for the operation. RowExistenceExpectation_IGNORE bypasses the row existence check. putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE) batchWriteRequest.AddRowChange(putRowChange)To add attribute columns on a put operation:
// Add an attribute column. putRowChange.AddColumn("col1", "val1") // Add an attribute column with a custom data version number. putRowChange.AddColumnWithTimestamp("col2", "val2", int64(time.Now().Unix() * 1000)) -
UpdateRowChange: Updates a row by changing attribute values, adding columns, or deleting a column or a specific version.updatePk := new(tablestore.PrimaryKey) updatePk.AddPrimaryKeyColumn("id", "row1") updateRowChange := new(tablestore.UpdateRowChange) updateRowChange.TableName = "test_table" updateRowChange.PrimaryKey = updatePk updateRowChange.PutColumn("col1", "changed_val1") // You must set a condition for the operation. RowExistenceExpectation_IGNORE bypasses the row existence check. updateRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE) batchWriteRequest.AddRowChange(updateRowChange)To add or delete attribute columns in the same update:
// Add an attribute column. updateRowChange.PutColumn("col3", "val3") // Add an attribute column with a custom data version number. updateRowChange.PutColumnWithTimestamp("col4", "val4", int64(time.Now().Unix() * 1000)) // Delete an attribute column. updateRowChange.DeleteColumn("col2") -
DeleteRowChange: Deletes a row by its primary key.deletePk := new(tablestore.PrimaryKey) deletePk.AddPrimaryKeyColumn("id", "row1") deleteRowChange := new(tablestore.DeleteRowChange) deleteRowChange.TableName = "test_table" deleteRowChange.PrimaryKey = deletePk // You must set a condition for the operation. RowExistenceExpectation_IGNORE bypasses the row existence check. deleteRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE) batchWriteRequest.AddRowChange(deleteRowChange)