批量更新数据

更新时间:
复制为 MD 格式

本文介绍如何通过 Go SDK 对表格存储的数据进行批量更新操作,包括写入数据、修改数据和删除数据,支持同时操作多个表的数据。

注意事项

  • 服务端检查到部分操作的参数错误时会抛出参数错误异常,此时该请求中的所有操作都将不执行。

  • 批量更新操作单次支持写入的最大行数为200行,且所有行的数据量总和不能超过4MB。

前提条件

初始化Tablestore Client

方法说明

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

BatchWriteRowRequest参数说明

名称

类型

说明

RowChangesGroupByTable(必选)

map[string][]RowChange

行数据操作列表,包括数据表名称和数据操作类型。数据操作类型包括写入数据、更新数据和删除数据。

示例代码

以下示例代码使用批量数据操作方法在 test_table 表中插入一行数据。

func BatchWriteRowSample(client *tablestore.TableStoreClient) {
    // 构造请求数据
    batchWriteRequest := new(tablestore.BatchWriteRowRequest)

    // 添加 PutRowChange
    putPk := new(tablestore.PrimaryKey)
    putPk.AddPrimaryKeyColumn("id", "row1")
    putRowChange := new(tablestore.PutRowChange)
    putRowChange.TableName = "test_table"
    putRowChange.PrimaryKey = putPk
    // 写入数据时必须指定写入条件 (RowExistenceExpectation_IGNORE,表示不做行存在性判断)
    putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
    batchWriteRequest.AddRowChange(putRowChange)

    // 调用 batchWriteRow 方法进行批量数据操作
    response, err := client.BatchWriteRow(batchWriteRequest)

    // 返回结果处理
    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)
                }
            }
        }
    }
}

不同类型的数据操作示例代码参考如下。

  • PutRowChange:写入行数据。

    putPk := new(tablestore.PrimaryKey)
    putPk.AddPrimaryKeyColumn("id", "row1")
    
    putRowChange := new(tablestore.PutRowChange)
    putRowChange.TableName = "test_table"
    putRowChange.PrimaryKey = putPk
    // 写入数据时必须指定写入条件 (RowExistenceExpectation_IGNORE,表示不做行存在性判断)
    putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
    batchWriteRequest.AddRowChange(putRowChange)

    写入行数据时添加属性列。

    // 添加属性列
    putRowChange.AddColumn("col1", "val1")
    // 添加自定义数据版本号的属性列
    putRowChange.AddColumnWithTimestamp("col2", "val2", int64(time.Now().Unix() * 1000))
  • UpdateRowChange:更新行数据,您可以修改属性列的值、添加数据列、删除属性列的某个版本或整个属性列。

    updatePk := new(tablestore.PrimaryKey)
    updatePk.AddPrimaryKeyColumn("id", "row1")
    
    updateRowChange := new(tablestore.UpdateRowChange)
    updateRowChange.TableName = "test_table"
    updateRowChange.PrimaryKey = updatePk
    updateRowChange.PutColumn("col1", "changed_val1")
    // 更新数据时必须指定更新条件 (RowExistenceExpectation_IGNORE,表示不做行存在性判断)
    updateRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
    batchWriteRequest.AddRowChange(updateRowChange)

    更新行数据时添加或删除属性列。

    // 添加属性列
    updateRowChange.PutColumn("col3", "val3")
    // 添加自定义数据版本号的属性列
    updateRowChange.PutColumnWithTimestamp("col4", "val4", int64(time.Now().Unix() * 1000))
    // 删除属性列
    updateRowChange.DeleteColumn("col2")
  • DeleteRowChange:删除行数据。

    deletePk := new(tablestore.PrimaryKey)
    deletePk.AddPrimaryKeyColumn("id", "row1")
    
    deleteRowChange := new(tablestore.DeleteRowChange)
    deleteRowChange.TableName = "test_table"
    deleteRowChange.PrimaryKey = deletePk
    // 删除数据时必须指定删除条件 (RowExistenceExpectation_IGNORE,表示不做行存在性判断)
    deleteRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
    batchWriteRequest.AddRowChange(deleteRowChange)

相关文档