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

Tablestore:データの一括更新

最終更新日:Mar 10, 2026

Tablestore PHP SDK の batchWriteRow を呼び出すことで、複数のテーブルにまたがるデータの挿入、更新、削除を一括で実行できます。

注意事項

  • サーバーがいずれかの操作でパラメーターエラーを検出した場合、リクエスト内のすべての操作が失敗します。

  • 1 回の一括更新操作でサポートされる行数は最大 200 行です。すべての行の合計データサイズは 4 MB を超えることはできません。

前提条件

Tablestore クライアントの初期化

メソッド

public function batchWriteRow(array $request)

$request パラメーター

  • tables (必須) array:行操作のリスト。次のパラメーターが含まれます。

    名前

    説明

    table_name (必須)

    文字列

    データテーブルの名前。

    rows (必須)

    配列

    書き込み、更新、削除などのデータ操作のタイプ。

  • transaction_id (任意) string:ローカルトランザクションの ID。この ID はローカルトランザクションを一意に識別します。詳細については、「ローカルトランザクション」をご参照ください。

次のコードは、test_table テーブルに行を挿入する例です。

$table = array (
    'table_name' => 'test_table',
    'rows' => array (
        array (
            'operation_type' => OperationTypeConst::CONST_PUT,
            'primary_key' => array ( array('id', 'row1') )
        )
    )
);
$request = array(
    'tables' => array (
        $table
    )
);

try {
    // batchWriteRow メソッドを呼び出して、一括データ操作を実行します。
    $response = $client->batchWriteRow ($request);

     // 応答を処理します。
    foreach ($response['tables'] as $tableGroup) {
        foreach ($tableGroup['rows'] as $row) {
            if (!$row['is_ok']) {
                echo "TableName: {$tableGroup['table_name']}. ErrorCode: {$row['error']['code']}. ErrorMessage: {$row['error']['message']} \n";
            }
        }
    }
} catch (Exception $e){
    echo 'Batch write row failed.';
}

さまざまな操作タイプの例については、以下をご参照ください。

  • PutRowItem:行を挿入します。

    $table = array (
        'table_name' => 'test_table',
        'rows' => array (
            array (
                'operation_type' => OperationTypeConst::CONST_PUT,
                'primary_key' => array ( array('id', 'row1') )
            )
        )
    );

    属性列を持つ行を挿入します。

    $table = array (
        'table_name' => 'test_table',
        'rows' => array (
            array (
                'operation_type' => OperationTypeConst::CONST_PUT,
                'primary_key' => array ( array('id', 'row1') ),
                'attribute_columns' => array (
                    array('col1', 'val1'),
                    array('col2', 'val2', null, intval(microtime(true) * 1000))
                )
            )
        )
    );
  • UpdateRowItem:属性列の値を変更したり、属性列を追加したり、属性列の特定のバージョンまたはすべてのバージョンを削除したりして、行を更新します。

    $table = array (
        'table_name' => 'test_table',
        'rows' => array (
            array (
                'operation_type' => OperationTypeConst::CONST_UPDATE,
                'primary_key' => array ( array('id', 'row1') ),
                'update_of_attribute_columns'=> array(
                    'PUT' => array ( array('col1', 'changed_val1') )
                ),
                // 行を更新するには条件が必要です。RowExistenceExpectation.IGNORE は行の存在チェックをスキップします。
                'condition' => RowExistenceExpectationConst::CONST_IGNORE
            )
        )
    );

    行の更新時に属性列を追加または削除します。

    $table = array (
        'table_name' => 'test_table',
        'rows' => array (
            array (
                'operation_type' => OperationTypeConst::CONST_UPDATE,
                'primary_key' => array ( array('id', 'row1') ),
                'update_of_attribute_columns'=> array(
                    'PUT' => array ( 
                        // 属性列を追加します。
                        array('col3', 'val3'),
                        // カスタムバージョン番号を持つ属性列を追加します。
                        array('col4', 'val4', null, intval(microtime(true) * 1000))
                    ),
                    // 属性列を削除します。
                    'DELETE_ALL' => array('col2')
                ),
                // 行を更新するには条件が必要です。RowExistenceExpectation.IGNORE は行の存在チェックをスキップします。
                'condition' => RowExistenceExpectationConst::CONST_IGNORE
            )
        )
    );
  • DeleteRowItem:行を削除します。

    $table = array (
        'table_name' => 'test_table',
        'rows' => array (
            array (
                'operation_type' => OperationTypeConst::CONST_DELETE,
                'primary_key' => array ( array('id', 'row1') ),
                // 行を削除するには条件が必要です。RowExistenceExpectation.IGNORE は行の存在チェックをスキップします。
                'condition' => RowExistenceExpectationConst::CONST_IGNORE
            )
        )
    );

参照