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(必选) |
|
按数据表名称组织的行操作。每个操作可以是 |
|
IsAtomic(可选) |
|
是否对同一数据表、同一分区键下的行执行原子批量写。默认值为 |
返回值
BatchWriteRowResponse 包含以下业务信息。
|
字段 |
类型 |
说明 |
|
|
|
按数据表名称组织的逐行结果。每个 |
场景示例
同时操作多张表
以下示例在一次请求中向一张数据表写入一行数据,并更新另一张数据表的一行数据。
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)
}
该文章对您有帮助吗?