すべてのプロダクト
Search
ドキュメントセンター

Tablestore:単一行の更新

最終更新日:Apr 01, 2026

このトピックでは、Go SDK を使用して Tablestore テーブルの単一行を更新する方法について説明します。属性列の追加または更新、属性列の特定のバージョンの削除、または属性列全体の削除が可能です。

前提条件

Tablestore クライアントの初期化

メソッド

func (tableStoreClient *TableStoreClient) UpdateRow(request *UpdateRowRequest) (*UpdateRowResponse, error)

UpdateRowRequest パラメーター

  • UpdateRowChange (必須) *UpdateRowChange: 行への変更を指定します。このパラメーターには、次のフィールドが含まれます。

    パラメーター

    説明

    TableName (必須)

    string

    テーブルの名前。

    PrimaryKey (必須)

    PrimaryKey

    行のプライマリキー。プライマリキー列の名前と値が含まれます。

    • プライマリキー列に有効なデータの型は、STRING 、 INTEGER 、 BINARY です。

    • プライマリキー列の数とデータの型は、テーブルのプライマリキー定義と一致する必要があります。

    Columns (必須)

    []ColumnToUpdate

    更新する属性列と更新操作のタイプ。

    Condition (必須)

    RowCondition

    更新条件。詳細については、「条件付き更新」をご参照ください。

    TransactionId (オプション)

    string

    ローカルトランザクション ID。詳細については、「ローカルトランザクション」をご参照ください。

サンプルコード

次のサンプルコードは、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")

関連トピック