Xamarin 的AWS行動 SDK 現在已包含在AWS SDK for .NET. 本指南參考 Xamarin 行動 SDK 的封存版本。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用文件模型
文檔模型提供了圍繞低級別 .NET API 的包裝類。表和文檔是關鍵的包裝類。您可以使用文件模型來建立、檢索、更新和刪除項目。若要建立、更新和刪除資料表,您必須使用低階 API。如需如何使用低階 API 的資訊,請參使用 DynamoDB 服務級別 API。低階 API 在亞馬遜 DynamoDB 中提供。DocumentModel命名空間。
若要進一步了解文件模型,請參.NET 文件模型。
建立 DynamoDB 用户端
若要建立 DynamoDB 用户端,請執行下列
var client = new AmazonDynamoDBClient(credentials,region); DynamoDBContext context = new DynamoDBContext(client);
CRUD 操作
儲存項目
建立項目:
Table table = Table.LoadTable(client, "Books"); id = Guid.NewGuid().ToString(); var books = new Document(); books["Id"] = id; books["Author"] = "Mark Twain"; books["Title"] = "Adventures of Huckleberry Finn"; books["ISBN"] = "112-111111"; books["Price"] = "10";
將某個項目保存到 DynamoDB 資料表:
var book = await table.PutItemAsync(books);
擷取項目
要檢索項目:
public async Task GetItemAsync(AWSCredentials credentials, RegionEndpoint region) { var client = new AmazonDynamoDBClient(credentials, region); Table books = Table.LoadTable(client, "Books"); var book = await books.GetItemAsync(id); }
更新項目
若要更新項目:
public async Task UpdateItemAttributesAsync(AWSCredentials credentials, RegionEndpoint region) { var book = new Document(); book["Id"] = id; book["PageCount"] = "200"; var client = new AmazonDynamoDBClient(credentials, region); Table books = Table.LoadTable(client, "Books"); Document updatedBook = await books.UpdateItemAsync(book); }
若要更新項目,請執行以下操作:
public async Task UpdateItemConditionallyAsync(AWSCredentials credentials, RegionEndpoint region) { var book = new Document(); book["Id"] = id; book["Price"] = "30"; // For conditional price update, creating a condition expression. Expression expr = new Expression(); expr.ExpressionStatement = "Price = :val"; expr.ExpressionAttributeValues[":val"] = 10.00; var client = new AmazonDynamoDBClient(credentials, region); Table books = Table.LoadTable(client, "Books"); Document updatedBook = await books.UpdateItemAsync(book); }
刪除項目
若要刪除項目:
public async Task DeleteItemAsync(AWSCredentials credentials, RegionEndpoint region) { var client = new AmazonDynamoDBClient(credentials, region); Table books = Table.LoadTable(client, "Books"); await books.DeleteItemAsync(id); }
查詢和掃描
要查詢和檢索作者為「馬克·吐温」的所有圖書,請執行以下操作:
public async Task QueryAsync(AWSCredentials credentials, RegionEndpoint region) { var client = new AmazonDynamoDBClient(credentials, region); Table books = Table.LoadTable(client, "Books"); var search = books.Query(new QueryOperationConfig() { IndexName = "Author-Title-index", Filter = new QueryFilter("Author", QueryOperator.Equal, "Mark Twain") }); Console.WriteLine("ScanAsync: printing query response"); var documents = await search.GetRemainingAsync(); documents.ForEach((d) = > { PrintDocument(d); }); }
下面的掃描示例代碼返回我們表格中的所有圖書:
public async Task ScanAsync(AWSCredentials credentials, RegionEndpoint region) { var client = new AmazonDynamoDBClient(credentials, region); Table books = Table.LoadTable(client, "Books"); var search = books.Scan(new ScanOperationConfig() { ConsistentRead = true }); Console.WriteLine("ScanAsync: printing scan response"); var documents = await search.GetRemainingAsync(); documents.ForEach((d) = > { PrintDocument(d); }); }