このトピックでは、.NET SDK を使用して Tablestore でバッチ書き込みを実行する方法について説明します。バッチ書き込みを使用すると、1 回のリクエストで複数のテーブルにまたがるデータの書き込み、更新、削除ができます。
注意事項
サーバーがいずれかの個別操作でパラメーターエラーを検出すると、例外がスローされます。バッチリクエスト全体が失敗し、操作は実行されません。
1 回のバッチ書き込み操作は、最大 200 行をサポートします。すべての行の合計データサイズは 4 MB を超えることはできません。
前提条件
メソッド
public BatchWriteRowResponse BatchWriteRow(BatchWriteRowRequest request)非同期メソッド:
public Task<BatchWriteRowResponse> BatchWriteRowAsync(BatchWriteRowRequest request)コード例
次のコードは、バッチ書き込み操作を使用して、test_table テーブルに行を挿入する方法を示しています。
try
{
// リクエストを構築します。
BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest();
// RowChanges を追加します。
RowChanges rowChanges = new RowChanges("test_table");
PrimaryKey primaryKey = new PrimaryKey
{
{ "id", new ColumnValue("row1") }
};
// 書き込み操作には条件が必須です。 `RowExistenceExpectation.IGNORE` は行の存在チェックをスキップすることを指定します。
Condition condition = new Condition(RowExistenceExpectation.IGNORE);
rowChanges.AddPut(condition, primaryKey, null);
batchWriteRowRequest.Add("test_table", rowChanges);
// BatchWriteRow メソッドを呼び出してバッチ書き込みを実行します。
BatchWriteRowResponse batchWriteRowResponse = client.BatchWriteRow(batchWriteRowRequest);
// レスポンスを処理します。
Console.WriteLine($"RequestId: {batchWriteRowResponse.RequestID}");
if (!batchWriteRowResponse.IsAllSucceed)
{
foreach (var item in batchWriteRowResponse.GetFailedRows())
{
Console.WriteLine($"テーブル名: {item.TableName}、エラーメッセージ: {item.ErrorMessage}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"行のバッチ書き込みに失敗しました。例外: {ex.Message}");
}次のコードは、さまざまなタイプのデータ操作の例を示しています。
RowPutChange:行を書き込みます。PrimaryKey primaryKey = new PrimaryKey { { "id", new ColumnValue("row1") } }; // 書き込み操作には条件が必須です。 `RowExistenceExpectation.IGNORE` は行の存在チェックをスキップすることを指定します。 Condition condition = new Condition(RowExistenceExpectation.IGNORE); rowChanges.AddPut(condition, primaryKey, null);行を書き込むときに属性列を追加することもできます。
PrimaryKey primaryKey = new PrimaryKey { { "id", new ColumnValue("row1") } }; AttributeColumns columns = new AttributeColumns { { "col1", new ColumnValue("val1") } }; // 書き込み操作には条件が必須です。 `RowExistenceExpectation.IGNORE` は行の存在チェックをスキップすることを指定します。 Condition condition = new Condition(RowExistenceExpectation.IGNORE); rowChanges.AddPut(condition, primaryKey, columns);RowUpdateChange:行を更新します。属性列の値を変更したり、属性列を追加したり、属性列の特定のバージョンまたは属性列全体を削除したりできます。PrimaryKey primaryKey = new PrimaryKey { { "id", new ColumnValue("row1") } }; UpdateOfAttribute updateOfAttribute = new UpdateOfAttribute(); updateOfAttribute.AddAttributeColumnToPut("col1", new ColumnValue("changed_val1")); // 更新操作には条件が必須です。 `RowExistenceExpectation.IGNORE` は行の存在チェックをスキップすることを指定します。 Condition condition = new Condition(RowExistenceExpectation.IGNORE); rowChanges.AddUpdate(condition, primaryKey, updateOfAttribute);行を更新するときに属性列を追加または削除することもできます。
PrimaryKey primaryKey = new PrimaryKey { { "id", new ColumnValue("row1") } }; UpdateOfAttribute updateOfAttribute = new UpdateOfAttribute(); // 属性列を追加します。 updateOfAttribute.AddAttributeColumnToPut("col3", new ColumnValue("val3")); // 属性列を削除します。 updateOfAttribute.AddAttributeColumnToDelete("col2"); // 更新操作には条件が必須です。 `RowExistenceExpectation.IGNORE` は行の存在チェックをスキップすることを指定します。 Condition condition = new Condition(RowExistenceExpectation.IGNORE); rowChanges.AddUpdate(condition, primaryKey, updateOfAttribute);RowDeleteChange:行を削除します。PrimaryKey primaryKey = new PrimaryKey { { "id", new ColumnValue("row1") } }; // 削除操作には条件が必須です。 `RowExistenceExpectation.IGNORE` は行の存在チェックをスキップすることを指定します。 Condition condition = new Condition(RowExistenceExpectation.IGNORE); rowChanges.AddDelete(condition, primaryKey);