Batch row operations

Updated at:

Use the Tablestore SDK for Go to batch write, update, or delete rows in one or more Wide Column model tables.

Prerequisites

Install the Tablestore SDK for Go and initialize the client.

Description

Call BatchWriteRow to batch perform write, update, and delete operations. A call can contain up to 200 rows, and the total data size cannot exceed 4 MB. By default, each row operation is independent. If the server detects a request-level parameter error, no operation in the batch is performed. Check each row result and handle failed operations.

func (tableStoreClient *TableStoreClient) BatchWriteRow(request *BatchWriteRowRequest) (*BatchWriteRowResponse, error)

The following sample writes one row, updates one row, and deletes one row in the same request.

request := &tablestore.BatchWriteRowRequest{}

putChange := &tablestore.PutRowChange{TableName: "example_table"}
putChange.PrimaryKey = &tablestore.PrimaryKey{}
putChange.PrimaryKey.AddPrimaryKeyColumn("id", "row1")
putChange.AddColumn("status", "created")
putChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
request.AddRowChange(putChange)

updateChange := &tablestore.UpdateRowChange{TableName: "example_table"}
updateChange.PrimaryKey = &tablestore.PrimaryKey{}
updateChange.PrimaryKey.AddPrimaryKeyColumn("id", "row2")
updateChange.PutColumn("status", "updated")
updateChange.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
request.AddRowChange(updateChange)

deleteChange := &tablestore.DeleteRowChange{TableName: "example_table"}
deleteChange.PrimaryKey = &tablestore.PrimaryKey{}
deleteChange.PrimaryKey.AddPrimaryKeyColumn("id", "row3")
deleteChange.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
request.AddRowChange(deleteChange)

response, err := client.BatchWriteRow(request)
if err != nil {
    log.Fatal(err)
}

for _, row := range response.TableToRowsResult["example_table"] {
    fmt.Println(row.IsSucceed, row.Error)
}

Parameters

BatchWriteRowRequest contains the following parameters.

Name

Type

Description

RowChangesGroupByTable (required)

map[string][]RowChange

The row operations grouped by table name. Each operation can be a PutRowChange, UpdateRowChange, or DeleteRowChange. Call AddRowChange to add an operation.

IsAtomic (optional)

bool

Specifies whether to atomically write rows in the same table and under the same partition key. Default value: false. If this parameter is set to true, all row operations in the atomic batch either succeed or fail.

The parameters of each RowChange are the same as those of the corresponding single-row operation. For more information, see Write a row, Update a row, and Delete a row.

Response

BatchWriteRowResponse contains the following business information.

Field

Type

Description

TableToRowsResult

map[string][]RowResult

The row results grouped by table name. Each RowResult contains the success status, error information, and row index in the request. Depending on the operation configuration, the result may also contain a primary key or updated attribute columns.

Examples

Operate on multiple tables

The following sample writes a row to one table and updates a row in another table in one request.

request := &tablestore.BatchWriteRowRequest{}

putKey := &tablestore.PrimaryKey{}
putKey.AddPrimaryKeyColumn("id", "row1")
putChange := &tablestore.PutRowChange{TableName: "example_table", PrimaryKey: putKey}
putChange.AddColumn("status", "pending")
putChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
request.AddRowChange(putChange)

updateKey := &tablestore.PrimaryKey{}
updateKey.AddPrimaryKeyColumn("id", "row2")
updateChange := &tablestore.UpdateRowChange{TableName: "example_table_2", PrimaryKey: updateKey}
updateChange.PutColumn("status", "processed")
updateChange.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
request.AddRowChange(updateChange)

response, err := client.BatchWriteRow(request)
if err != nil {
    log.Fatal(err)
}
for tableName, rows := range response.TableToRowsResult {
    for _, row := range rows {
        fmt.Println(tableName, row.IsSucceed, row.Error)
    }
}

Atomic batch write

The following sample configures two updates under the same partition key as an atomic batch write.

request := &tablestore.BatchWriteRowRequest{IsAtomic: true}

for _, recordID := range []int64{1, 2} {
    primaryKey := &tablestore.PrimaryKey{}
    primaryKey.AddPrimaryKeyColumn("user_id", "user-a")
    primaryKey.AddPrimaryKeyColumn("record_id", recordID)

    change := &tablestore.UpdateRowChange{
        TableName:  "example_table",
        PrimaryKey: primaryKey,
    }
    change.PutColumn("status", "processed")
    change.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
    request.AddRowChange(change)
}

_, err := client.BatchWriteRow(request)
if err != nil {
    log.Fatal(err)
}