批量数据操作

更新时间:
复制 MD 格式

Go SDK 可在一次请求中批量写入、更新或删除一个或多个宽表模型数据表中的行数据。

前提条件

安装Tablestore Go SDK并初始化客户端。

功能说明

调用 BatchWriteRow 批量执行写入、更新和删除操作。单次调用最多包含 200 行,所有行的数据总量不能超过 4 MB。默认情况下各行独立执行;若服务端在请求级别发现参数错误,整批操作均不执行。需逐行检查返回结果并处理失败操作。

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

以下示例在同一请求中写入一行、更新一行并删除一行。

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)
}

参数说明

BatchWriteRowRequest 包含以下参数。

名称

类型

说明

RowChangesGroupByTable(必选)

map[string][]RowChange

按数据表名称组织的行操作。每个操作可以是 PutRowChangeUpdateRowChangeDeleteRowChange。可调用 AddRowChange 添加操作。

IsAtomic(可选)

bool

是否对同一数据表、同一分区键下的行执行原子批量写。默认值为 false。设置为 true 时,同一原子批次中的所有行操作全部成功或全部失败。

每种 RowChange 的参数与对应单行操作相同,具体请参见写入单行数据更新单行数据删除单行数据

返回值

BatchWriteRowResponse 包含以下业务信息。

字段

类型

说明

TableToRowsResult

map[string][]RowResult

按数据表名称组织的逐行结果。每个 RowResult 包含成功状态、错误信息和原请求中的行序号。根据操作配置,还可能返回主键或更新后的属性列。

场景示例

同时操作多张表

以下示例在一次请求中向一张数据表写入一行数据,并更新另一张数据表的一行数据。

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)
    }
}

原子批量写

以下示例将同一分区键下的两个更新操作设置为原子批量写。

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)
}