このトピックでは、.NET SDK を使用して Tablestore テーブルの単一行を更新する方法について説明します。属性列の値を更新したり、属性列を追加したり、特定のバージョンや属性列全体を削除したりできます。
前提条件
メソッド
public UpdateRowResponse UpdateRow(UpdateRowRequest request)非同期メソッド:
public Task<UpdateRowResponse> UpdateRowAsync(UpdateRowRequest request)サンプルコード
この例では、プライマリキーが row1 の行にある属性列 col1 の値を changed_val1 に更新します。
try
{
// プライマリキーを構築します。
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);
// `UpdateRow` メソッドを呼び出して行を更新します。
UpdateRowRequest updateRowRequest = new UpdateRowRequest("test_table", condition, primaryKey, updateOfAttribute);
UpdateRowResponse updateRowResponse = client.UpdateRow(updateRowRequest);
Console.WriteLine($"RequestId: {updateRowResponse.RequestID}");
Console.WriteLine($"読み取り CU コスト: {updateRowResponse.ConsumedCapacityUnit.Read}");
Console.WriteLine($"書き込み CU コスト: {updateRowResponse.ConsumedCapacityUnit.Write}");
}
catch (Exception ex)
{
Console.WriteLine($"行の更新に失敗しました。例外: {ex.Message}");
}サンプルコードを変更することで、次の行操作を実行できます。
属性列の追加
updateOfAttribute.AddAttributeColumnToPut("col2", new ColumnValue("val2"));属性列全体の削除
updateOfAttribute.AddAttributeColumnToDelete("col2");