全部產品
Search
文件中心

Tablestore:批次更新資料

更新時間:Apr 01, 2026

本文介紹如何通過 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)

相關文檔