AWS SDK for .NET 现在包括适用于 Xamarin 的 AWS Mobile SDK。本指南参考了适用于 Xamarin 的 Mobile SDK 的存档版本。
使用文档模型
文档模型围绕低级别 .NET API 提供封装类。表和文档是主要的封装类。您可以使用文档模型创建、检索、更新和删除项目。要创建、更新和删除表,您必须使用低级别 API。有关如何使用低级别 API 的说明,请参阅使用 DynamoDB 服务级别 API。低级别 API 在 Amazon.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); }
查询和扫描
查询和检索作者为“Mark Twain”的所有图书:
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); }); }