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

Tablestore:バッチ行操作

最終更新日:Aug 13, 2026

Tablestore SDK for Go を使用して、1 つまたは複数の Wide Column モデルのテーブルで、行の書き込み、更新、削除をバッチで実行します。

前提条件

Tablestore SDK for Go をインストールし、クライアントを初期化します。

説明

BatchWriteRow を呼び出して、書き込み、更新、削除操作をバッチで実行します。1 回の呼び出しには最大 200 行を含めることができ、合計データサイズは 4 MB を超えることはできません。デフォルトでは、各行操作は独立しています。サーバーがリクエストレベルのパラメーターエラーを検出した場合、バッチ内のどの操作も実行されません。各行の結果を確認し、失敗した操作を処理する必要があります。

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

次の例では、同じリクエスト内で 1 行を書き込み、1 行を更新し、1 行を削除します。

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 (必須)

map[string][]RowChange

テーブル名でグループ化された行操作。各操作はPutRowChangeUpdateRowChange、またはDeleteRowChange のいずれかです。AddRowChange を呼び出して操作を追加します。

IsAtomic (オプション)

bool

同じテーブルの同じパーティションキーを持つ行をアトミックに書き込むかどうかを指定します。デフォルト値: false。このパラメーターを true に設定すると、アトミックなバッチ内のすべての行操作が成功するか、すべて失敗します。

RowChange のパラメーターは、対応する単一行操作のパラメーターと同じです。詳細については、「行の書き込み」、「行の更新」、および「行の削除」をご参照ください。

レスポンス

BatchWriteRowResponse には次のビジネス情報があります。

フィールド

タイプ

説明

TableToRowsResult

map[string][]RowResult

テーブル名でグループ化された行の結果。各 RowResult は、成功ステータス、エラー情報、およびリクエスト内の行インデックスを含みます。操作の設定によっては、結果にプライマリキーまたは更新された属性列が含まれる場合があります。

複数のテーブルに対する操作

次の例では、1 つのリクエストで 1 つのテーブルに行を書き込み、別のテーブルで行を更新します。

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

アトミックなバッチ書き込み

次の例では、同じパーティションキーを持つ 2 つの更新をアトミックなバッチ書き込みとして設定します。

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