本文介紹如何通過 .NET SDK 對錶格儲存的資料進行批次更新操作,包括寫入資料、修改資料和刪除資料,支援同時操作多個表的資料。
注意事項
服務端檢查到部分操作的參數錯誤時會拋出參數錯誤異常,此時該請求中的所有操作都將不執行。
批次更新操作單次支援寫入的最大行數為200行,且所有行的資料量總和不能超過4MB。
前提條件
方法說明
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($"Table name: {item.TableName}. Error message: {item.ErrorMessage}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Batch write row failed, exception: {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);