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