このトピックでは、Go SDK を使用して Tablestore テーブルの単一行を更新する方法について説明します。属性列の追加または更新、属性列の特定のバージョンの削除、または属性列全体の削除が可能です。
前提条件
メソッド
func (tableStoreClient *TableStoreClient) UpdateRow(request *UpdateRowRequest) (*UpdateRowResponse, error)サンプルコード
次のサンプルコードは、test_table テーブルの行を更新します。プライマリキー値が row1 の行について、col1 属性列の値を changed_val1 に変更します。
func UpdateRowSample(client *tablestore.TableStoreClient) {
// プライマリキーを構築します。
updatePk := new(tablestore.PrimaryKey)
updatePk.AddPrimaryKeyColumn("id", "row1")
// 行の更新データを構築します。
updateRowChange := new(tablestore.UpdateRowChange)
updateRowChange.TableName = "test_table"
updateRowChange.PrimaryKey = updatePk
updateRowChange.PutColumn("col1", "changed_val1")
// 行を更新する際には条件が必須です。
// tablestore.RowExistenceExpectation_IGNORE は、行の存在チェックが実行されないことを意味します。
updateRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
// UpdateRow メソッドを呼び出して行を更新します。
updateRowRequest := new(tablestore.UpdateRowRequest)
updateRowRequest.UpdateRowChange = updateRowChange
response, err := client.UpdateRow(updateRowRequest)
if err != nil {
fmt.Println("Update row failed with error: ", err)
} else {
fmt.Printf("RequestId: %s \n", response.RequestId)
fmt.Printf("Read CU Cost: %d \n", response.ConsumedCapacityUnit.Read)
fmt.Printf("Write CU Cost: %d \n", response.ConsumedCapacityUnit.Write)
}
}以下の行操作も実行できます。
属性列を追加します。
updateRowChange.PutColumn("col2", "val2")属性列のタイムスタンプ (バージョン) を設定します。
updateRowChange.PutColumnWithTimestamp("col2", "val2", int64(time.Now().Unix() * 1000)) updateRowChange.PutColumnWithTimestamp("col2", int64(1), int64(1758249013000))属性列の特定のバージョンを削除します。
updateRowChange.DeleteColumnWithTimestamp("col2", 1747893563831)属性列全体を削除します。
updateRowChange.DeleteColumn("col2")