使用 Amazon SDK for .NET 的 DynamoDB 示例 - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon SDK for .NET 的 DynamoDB 示例

以下代码示例演示了如何将Amazon SDK for .NET 与 DynamoDB 结合使用,以执行操作和实现常见场景。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景是指显示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。

每个示例都包含一个指向的链接 GitHub,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

开始使用

以下代码示例演示了如何开始使用 DynamoDB。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.Model; namespace DynamoDB_Actions; public static class HelloDynamoDB { static async Task Main(string[] args) { var dynamoDbClient = new AmazonDynamoDBClient(); Console.WriteLine($"Hello Amazon Dynamo DB! Following are some of your tables:"); Console.WriteLine(); // You can use await and any of the async methods to get a response. // Let's get the first five tables. var response = await dynamoDbClient.ListTablesAsync( new ListTablesRequest() { Limit = 5 }); foreach (var table in response.TableNames) { Console.WriteLine($"\tTable: {table}"); Console.WriteLine(); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考ListTables中的。

操作

以下代码示例演示了如何创建 DynamoDB 表。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Creates a new Amazon DynamoDB table and then waits for the new /// table to become active. /// </summary> /// <param name="client">An initialized Amazon DynamoDB client object.</param> /// <param name="tableName">The name of the table to create.</param> /// <returns>A Boolean value indicating the success of the operation.</returns> public static async Task<bool> CreateMovieTableAsync(AmazonDynamoDBClient client, string tableName) { var response = await client.CreateTableAsync(new CreateTableRequest { TableName = tableName, AttributeDefinitions = new List<AttributeDefinition>() { new AttributeDefinition { AttributeName = "title", AttributeType = ScalarAttributeType.S, }, new AttributeDefinition { AttributeName = "year", AttributeType = ScalarAttributeType.N, }, }, KeySchema = new List<KeySchemaElement>() { new KeySchemaElement { AttributeName = "year", KeyType = KeyType.HASH, }, new KeySchemaElement { AttributeName = "title", KeyType = KeyType.RANGE, }, }, ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 5, WriteCapacityUnits = 5, }, }); // Wait until the table is ACTIVE and then report success. Console.Write("Waiting for table to become active..."); var request = new DescribeTableRequest { TableName = response.TableDescription.TableName, }; TableStatus status; int sleepDuration = 2000; do { System.Threading.Thread.Sleep(sleepDuration); var describeTableResponse = await client.DescribeTableAsync(request); status = describeTableResponse.Table.TableStatus; Console.Write("."); } while (status != "ACTIVE"); return status == TableStatus.ACTIVE; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考CreateTable中的。

以下代码示例演示了如何删除 DynamoDB 表。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

public static async Task<bool> DeleteTableAsync(AmazonDynamoDBClient client, string tableName) { var request = new DeleteTableRequest { TableName = tableName, }; var response = await client.DeleteTableAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Table {response.TableDescription.TableName} successfully deleted."); return true; } else { Console.WriteLine("Could not delete table."); return false; } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DeleteTable中的。

以下代码示例演示了如何删除 DynamoDB 表中的项目。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Deletes a single item from a DynamoDB table. /// </summary> /// <param name="client">The initialized DynamoDB client object.</param> /// <param name="tableName">The name of the table from which the item /// will be deleted.</param> /// <param name="movieToDelete">A movie object containing the title and /// year of the movie to delete.</param> /// <returns>A Boolean value indicating the success or failure of the /// delete operation.</returns> public static async Task<bool> DeleteItemAsync( AmazonDynamoDBClient client, string tableName, Movie movieToDelete) { var key = new Dictionary<string, AttributeValue> { ["title"] = new AttributeValue { S = movieToDelete.Title }, ["year"] = new AttributeValue { N = movieToDelete.Year.ToString() }, }; var request = new DeleteItemRequest { TableName = tableName, Key = key, }; var response = await client.DeleteItemAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DeleteItem中的。

以下代码示例演示了如何获取一批 DynamoDB 项目。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Collections.Generic; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.Model; namespace LowLevelBatchGet { public class LowLevelBatchGet { private static readonly string _table1Name = "Forum"; private static readonly string _table2Name = "Thread"; public static async void RetrieveMultipleItemsBatchGet(AmazonDynamoDBClient client) { var request = new BatchGetItemRequest { RequestItems = new Dictionary<string, KeysAndAttributes>() { { _table1Name, new KeysAndAttributes { Keys = new List<Dictionary<string, AttributeValue> >() { new Dictionary<string, AttributeValue>() { { "Name", new AttributeValue { S = "Amazon DynamoDB" } } }, new Dictionary<string, AttributeValue>() { { "Name", new AttributeValue { S = "Amazon S3" } } } } }}, { _table2Name, new KeysAndAttributes { Keys = new List<Dictionary<string, AttributeValue> >() { new Dictionary<string, AttributeValue>() { { "ForumName", new AttributeValue { S = "Amazon DynamoDB" } }, { "Subject", new AttributeValue { S = "DynamoDB Thread 1" } } }, new Dictionary<string, AttributeValue>() { { "ForumName", new AttributeValue { S = "Amazon DynamoDB" } }, { "Subject", new AttributeValue { S = "DynamoDB Thread 2" } } }, new Dictionary<string, AttributeValue>() { { "ForumName", new AttributeValue { S = "Amazon S3" } }, { "Subject", new AttributeValue { S = "S3 Thread 1" } } } } } } } }; BatchGetItemResponse response; do { Console.WriteLine("Making request"); response = await client.BatchGetItemAsync(request); // Check the response. var responses = response.Responses; // Attribute list in the response. foreach (var tableResponse in responses) { var tableResults = tableResponse.Value; Console.WriteLine("Items retrieved from table {0}", tableResponse.Key); foreach (var item1 in tableResults) { PrintItem(item1); } } // Any unprocessed keys? could happen if you exceed ProvisionedThroughput or some other error. Dictionary<string, KeysAndAttributes> unprocessedKeys = response.UnprocessedKeys; foreach (var unprocessedTableKeys in unprocessedKeys) { // Print table name. Console.WriteLine(unprocessedTableKeys.Key); // Print unprocessed primary keys. foreach (var key in unprocessedTableKeys.Value.Keys) { PrintItem(key); } } request.RequestItems = unprocessedKeys; } while (response.UnprocessedKeys.Count > 0); } private static void PrintItem(Dictionary<string, AttributeValue> attributeList) { foreach (KeyValuePair<string, AttributeValue> kvp in attributeList) { string attributeName = kvp.Key; AttributeValue value = kvp.Value; Console.WriteLine( attributeName + " " + (value.S == null ? "" : "S=[" + value.S + "]") + (value.N == null ? "" : "N=[" + value.N + "]") + (value.SS == null ? "" : "SS=[" + string.Join(",", value.SS.ToArray()) + "]") + (value.NS == null ? "" : "NS=[" + string.Join(",", value.NS.ToArray()) + "]") ); } Console.WriteLine("************************************************"); } static void Main() { var client = new AmazonDynamoDBClient(); RetrieveMultipleItemsBatchGet(client); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考BatchGetItem中的。

以下代码示例演示了如何从 DynamoDB 表中获取项目。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Gets information about an existing movie from the table. /// </summary> /// <param name="client">An initialized Amazon DynamoDB client object.</param> /// <param name="newMovie">A Movie object containing information about /// the movie to retrieve.</param> /// <param name="tableName">The name of the table containing the movie.</param> /// <returns>A Dictionary object containing information about the item /// retrieved.</returns> public static async Task<Dictionary<string, AttributeValue>> GetItemAsync(AmazonDynamoDBClient client, Movie newMovie, string tableName) { var key = new Dictionary<string, AttributeValue> { ["title"] = new AttributeValue { S = newMovie.Title }, ["year"] = new AttributeValue { N = newMovie.Year.ToString() }, }; var request = new GetItemRequest { Key = key, TableName = tableName, }; var response = await client.GetItemAsync(request); return response.Item; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考GetItem中的。

以下代码示例演示了如何获取有关 DynamoDB 表的信息。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

private static async Task GetTableInformation() { Console.WriteLine("\n*** Retrieving table information ***"); var response = await Client.DescribeTableAsync(new DescribeTableRequest { TableName = ExampleTableName }); var table = response.Table; Console.WriteLine($"Name: {table.TableName}"); Console.WriteLine($"# of items: {table.ItemCount}"); Console.WriteLine($"Provision Throughput (reads/sec): " + $"{table.ProvisionedThroughput.ReadCapacityUnits}"); Console.WriteLine($"Provision Throughput (writes/sec): " + $"{table.ProvisionedThroughput.WriteCapacityUnits}"); }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DescribeTable中的。

以下代码示例演示了如何列出 DynamoDB 表。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

private static async Task ListMyTables() { Console.WriteLine("\n*** Listing tables ***"); string lastTableNameEvaluated = null; do { var response = await Client.ListTablesAsync(new ListTablesRequest { Limit = 2, ExclusiveStartTableName = lastTableNameEvaluated }); foreach (var name in response.TableNames) { Console.WriteLine(name); } lastTableNameEvaluated = response.LastEvaluatedTableName; } while (lastTableNameEvaluated != null); }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考ListTables中的。

以下代码示例演示了如何将项目放入 DynamoDB 表中。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Adds a new item to the table. /// </summary> /// <param name="client">An initialized Amazon DynamoDB client object.</param> /// <param name="newMovie">A Movie object containing informtation for /// the movie to add to the table.</param> /// <param name="tableName">The name of the table where the item will be added.</param> /// <returns>A Boolean value that indicates the results of adding the item.</returns> public static async Task<bool> PutItemAsync(AmazonDynamoDBClient client, Movie newMovie, string tableName) { var item = new Dictionary<string, AttributeValue> { ["title"] = new AttributeValue { S = newMovie.Title }, ["year"] = new AttributeValue { N = newMovie.Year.ToString() }, }; var request = new PutItemRequest { TableName = tableName, Item = item, }; var response = await client.PutItemAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考PutItem中的。

以下代码示例演示了如何查询 DynamoDB 表。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Queries the table for movies released in a particular year and /// then displays the information for the movies returned. /// </summary> /// <param name="client">The initialized DynamoDB client object.</param> /// <param name="tableName">The name of the table to query.</param> /// <param name="year">The release year for which we want to /// view movies.</param> /// <returns>The number of movies that match the query.</returns> public static async Task<int> QueryMoviesAsync(AmazonDynamoDBClient client, string tableName, int year) { var movieTable = Table.LoadTable(client, tableName); var filter = new QueryFilter("year", QueryOperator.Equal, year); Console.WriteLine("\nFind movies released in: {year}:"); var config = new QueryOperationConfig() { Limit = 10, // 10 items per page. Select = SelectValues.SpecificAttributes, AttributesToGet = new List<string> { "title", "year", }, ConsistentRead = true, Filter = filter, }; // Value used to track how many movies match the // supplied criteria. var moviesFound = 0; Search search = movieTable.Query(config); do { var movieList = await search.GetNextSetAsync(); moviesFound += movieList.Count; foreach (var movie in movieList) { DisplayDocument(movie); } } while (!search.IsDone); return moviesFound; }
  • 有关 API 详细信息,请参阅 Amazon SDK for .NET API 参考中的 Query

以下代码示例演示了如何在 DynamoDB 表上运行 PartiQL 语句。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

使用 INSERT 语句添加项目。

/// <summary> /// Inserts a single movie into the movies table. /// </summary> /// <param name="tableName">The name of the table.</param> /// <param name="movieTitle">The title of the movie to insert.</param> /// <param name="year">The year that the movie was released.</param> /// <returns>A Boolean value that indicates the success or failure of /// the INSERT operation.</returns> public static async Task<bool> InsertSingleMovie(string tableName, string movieTitle, int year) { string insertBatch = $"INSERT INTO {tableName} VALUE {{'title': ?, 'year': ?}}"; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = insertBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = movieTitle }, new AttributeValue { N = year.ToString() }, }, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }

使用 SELECT 语句获取项目。

/// <summary> /// Uses a PartiQL SELECT statement to retrieve a single movie from the /// movie database. /// </summary> /// <param name="tableName">The name of the movie table.</param> /// <param name="movieTitle">The title of the movie to retrieve.</param> /// <returns>A list of movie data. If no movie matches the supplied /// title, the list is empty.</returns> public static async Task<List<Dictionary<string, AttributeValue>>> GetSingleMovie(string tableName, string movieTitle) { string selectSingle = $"SELECT * FROM {tableName} WHERE title = ?"; var parameters = new List<AttributeValue> { new AttributeValue { S = movieTitle }, }; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = selectSingle, Parameters = parameters, }); return response.Items; }

使用 SELECT 语句获取项目列表。

/// <summary> /// Retrieve multiple movies by year using a SELECT statement. /// </summary> /// <param name="tableName">The name of the movie table.</param> /// <param name="year">The year the movies were released.</param> /// <returns></returns> public static async Task<List<Dictionary<string, AttributeValue>>> GetMovies(string tableName, int year) { string selectSingle = $"SELECT * FROM {tableName} WHERE year = ?"; var parameters = new List<AttributeValue> { new AttributeValue { N = year.ToString() }, }; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = selectSingle, Parameters = parameters, }); return response.Items; }

使用 UPDATE 语句更新项目。

/// <summary> /// Updates a single movie in the table, adding information for the /// producer. /// </summary> /// <param name="tableName">the name of the table.</param> /// <param name="producer">The name of the producer.</param> /// <param name="movieTitle">The movie title.</param> /// <param name="year">The year the movie was released.</param> /// <returns>A Boolean value that indicates the success of the /// UPDATE operation.</returns> public static async Task<bool> UpdateSingleMovie(string tableName, string producer, string movieTitle, int year) { string insertSingle = $"UPDATE {tableName} SET Producer=? WHERE title = ? AND year = ?"; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = insertSingle, Parameters = new List<AttributeValue> { new AttributeValue { S = producer }, new AttributeValue { S = movieTitle }, new AttributeValue { N = year.ToString() }, }, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }

使用 DELETE 语句删除单个电影。

/// <summary> /// Deletes a single movie from the table. /// </summary> /// <param name="tableName">The name of the table.</param> /// <param name="movieTitle">The title of the movie to delete.</param> /// <param name="year">The year that the movie was released.</param> /// <returns>A Boolean value that indicates the success of the /// DELETE operation.</returns> public static async Task<bool> DeleteSingleMovie(string tableName, string movieTitle, int year) { var deleteSingle = $"DELETE FROM {tableName} WHERE title = ? AND year = ?"; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = deleteSingle, Parameters = new List<AttributeValue> { new AttributeValue { S = movieTitle }, new AttributeValue { N = year.ToString() }, }, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考ExecuteStatement中的。

以下代码示例演示了如何在 DynamoDB 表中运行批量 PartiQL 语句。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

使用批量 INSERT 语句添加项目。

/// <summary> /// Inserts movies imported from a JSON file into the movie table by /// using an Amazon DynamoDB PartiQL INSERT statement. /// </summary> /// <param name="tableName">The name of the table into which the movie /// information will be inserted.</param> /// <param name="movieFileName">The name of the JSON file that contains /// movie information.</param> /// <returns>A Boolean value that indicates the success or failure of /// the insert operation.</returns> public static async Task<bool> InsertMovies(string tableName, string movieFileName) { // Get the list of movies from the JSON file. var movies = ImportMovies(movieFileName); var success = false; if (movies is not null) { // Insert the movies in a batch using PartiQL. Because the // batch can contain a maximum of 25 items, insert 25 movies // at a time. string insertBatch = $"INSERT INTO {tableName} VALUE {{'title': ?, 'year': ?}}"; var statements = new List<BatchStatementRequest>(); try { for (var indexOffset = 0; indexOffset < 250; indexOffset += 25) { for (var i = indexOffset; i < indexOffset + 25; i++) { statements.Add(new BatchStatementRequest { Statement = insertBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = movies[i].Title }, new AttributeValue { N = movies[i].Year.ToString() }, }, }); } var response = await Client.BatchExecuteStatementAsync(new BatchExecuteStatementRequest { Statements = statements, }); // Wait between batches for movies to be successfully added. System.Threading.Thread.Sleep(3000); success = response.HttpStatusCode == System.Net.HttpStatusCode.OK; // Clear the list of statements for the next batch. statements.Clear(); } } catch (AmazonDynamoDBException ex) { Console.WriteLine(ex.Message); } } return success; } /// <summary> /// Loads the contents of a JSON file into a list of movies to be /// added to the DynamoDB table. /// </summary> /// <param name="movieFileName">The full path to the JSON file.</param> /// <returns>A generic list of movie objects.</returns> public static List<Movie> ImportMovies(string movieFileName) { if (!File.Exists(movieFileName)) { return null!; } using var sr = new StreamReader(movieFileName); string json = sr.ReadToEnd(); var allMovies = JsonConvert.DeserializeObject<List<Movie>>(json); if (allMovies is not null) { // Return the first 250 entries. return allMovies.GetRange(0, 250); } else { return null!; } }

使用批量 SELECT 语句获取项目。

/// <summary> /// Gets movies from the movie table by /// using an Amazon DynamoDB PartiQL SELECT statement. /// </summary> /// <param name="tableName">The name of the table.</param> /// <param name="title1">The title of the first movie.</param> /// <param name="title2">The title of the second movie.</param> /// <param name="year1">The year of the first movie.</param> /// <param name="year2">The year of the second movie.</param> /// <returns>True if successful.</returns> public static async Task<bool> GetBatch( string tableName, string title1, string title2, int year1, int year2) { var getBatch = $"SELECT FROM {tableName} WHERE title = ? AND year = ?"; var statements = new List<BatchStatementRequest> { new BatchStatementRequest { Statement = getBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = title1 }, new AttributeValue { N = year1.ToString() }, }, }, new BatchStatementRequest { Statement = getBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = title2 }, new AttributeValue { N = year2.ToString() }, }, } }; var response = await Client.BatchExecuteStatementAsync(new BatchExecuteStatementRequest { Statements = statements, }); if (response.Responses.Count > 0) { response.Responses.ForEach(r => { Console.WriteLine($"{r.Item["title"]}\t{r.Item["year"]}"); }); return true; } else { Console.WriteLine($"Couldn't find either {title1} or {title2}."); return false; } }

使用批量 UPDATE 语句更新项目。

/// <summary> /// Updates information for multiple movies. /// </summary> /// <param name="tableName">The name of the table containing the /// movies to be updated.</param> /// <param name="producer1">The producer name for the first movie /// to update.</param> /// <param name="title1">The title of the first movie.</param> /// <param name="year1">The year that the first movie was released.</param> /// <param name="producer2">The producer name for the second /// movie to update.</param> /// <param name="title2">The title of the second movie.</param> /// <param name="year2">The year that the second movie was released.</param> /// <returns>A Boolean value that indicates the success of the update.</returns> public static async Task<bool> UpdateBatch( string tableName, string producer1, string title1, int year1, string producer2, string title2, int year2) { string updateBatch = $"UPDATE {tableName} SET Producer=? WHERE title = ? AND year = ?"; var statements = new List<BatchStatementRequest> { new BatchStatementRequest { Statement = updateBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = producer1 }, new AttributeValue { S = title1 }, new AttributeValue { N = year1.ToString() }, }, }, new BatchStatementRequest { Statement = updateBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = producer2 }, new AttributeValue { S = title2 }, new AttributeValue { N = year2.ToString() }, }, } }; var response = await Client.BatchExecuteStatementAsync(new BatchExecuteStatementRequest { Statements = statements, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }

使用批量 DELETE 语句删除项目。

/// <summary> /// Deletes multiple movies using a PartiQL BatchExecuteAsync /// statement. /// </summary> /// <param name="tableName">The name of the table containing the /// moves that will be deleted.</param> /// <param name="title1">The title of the first movie.</param> /// <param name="year1">The year the first movie was released.</param> /// <param name="title2">The title of the second movie.</param> /// <param name="year2">The year the second movie was released.</param> /// <returns>A Boolean value indicating the success of the operation.</returns> public static async Task<bool> DeleteBatch( string tableName, string title1, int year1, string title2, int year2) { string updateBatch = $"DELETE FROM {tableName} WHERE title = ? AND year = ?"; var statements = new List<BatchStatementRequest> { new BatchStatementRequest { Statement = updateBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = title1 }, new AttributeValue { N = year1.ToString() }, }, }, new BatchStatementRequest { Statement = updateBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = title2 }, new AttributeValue { N = year2.ToString() }, }, } }; var response = await Client.BatchExecuteStatementAsync(new BatchExecuteStatementRequest { Statements = statements, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }

以下代码示例演示了如何扫描 DynamoDB 表。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

public static async Task<int> ScanTableAsync( AmazonDynamoDBClient client, string tableName, int startYear, int endYear) { var request = new ScanRequest { TableName = tableName, ExpressionAttributeNames = new Dictionary<string, string> { { "#yr", "year" }, }, ExpressionAttributeValues = new Dictionary<string, AttributeValue> { { ":y_a", new AttributeValue { N = startYear.ToString() } }, { ":y_z", new AttributeValue { N = endYear.ToString() } }, }, FilterExpression = "#yr between :y_a and :y_z", ProjectionExpression = "#yr, title, info.actors[0], info.directors, info.running_time_secs", Limit = 10 // Set a limit to demonstrate using the LastEvaluatedKey. }; // Keep track of how many movies were found. int foundCount = 0; var response = new ScanResponse(); do { response = await client.ScanAsync(request); foundCount += response.Items.Count; response.Items.ForEach(i => DisplayItem(i)); request.ExclusiveStartKey = response.LastEvaluatedKey; } while (response.LastEvaluatedKey.Count > 0); return foundCount; }
  • 有关 API 详细信息,请参阅 Amazon SDK for .NET API 参考中的 Scan

以下代码示例演示了如何更新 DynamoDB 表中的项目。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <summary> /// Updates an existing item in the movies table. /// </summary> /// <param name="client">An initialized Amazon DynamoDB client object.</param> /// <param name="newMovie">A Movie object containing information for /// the movie to update.</param> /// <param name="newInfo">A MovieInfo object that contains the /// information that will be changed.</param> /// <param name="tableName">The name of the table that contains the movie.</param> /// <returns>A Boolean value that indicates the success of the operation.</returns> public static async Task<bool> UpdateItemAsync( AmazonDynamoDBClient client, Movie newMovie, MovieInfo newInfo, string tableName) { var key = new Dictionary<string, AttributeValue> { ["title"] = new AttributeValue { S = newMovie.Title }, ["year"] = new AttributeValue { N = newMovie.Year.ToString() }, }; var updates = new Dictionary<string, AttributeValueUpdate> { ["info.plot"] = new AttributeValueUpdate { Action = AttributeAction.PUT, Value = new AttributeValue { S = newInfo.Plot }, }, ["info.rating"] = new AttributeValueUpdate { Action = AttributeAction.PUT, Value = new AttributeValue { N = newInfo.Rank.ToString() }, }, }; var request = new UpdateItemRequest { AttributeUpdates = updates, Key = key, TableName = tableName, }; var response = await client.UpdateItemAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考UpdateItem中的。

以下代码示例演示了如何写入一批 DynamoDB 项目。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

将一批项目写入影片表。

/// <summary> /// Loads the contents of a JSON file into a list of movies to be /// added to the DynamoDB table. /// </summary> /// <param name="movieFileName">The full path to the JSON file.</param> /// <returns>A generic list of movie objects.</returns> public static List<Movie> ImportMovies(string movieFileName) { if (!File.Exists(movieFileName)) { return null; } using var sr = new StreamReader(movieFileName); string json = sr.ReadToEnd(); var allMovies = JsonSerializer.Deserialize<List<Movie>>( json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); // Now return the first 250 entries. return allMovies.GetRange(0, 250); } /// <summary> /// Writes 250 items to the movie table. /// </summary> /// <param name="client">The initialized DynamoDB client object.</param> /// <param name="movieFileName">A string containing the full path to /// the JSON file containing movie data.</param> /// <returns>A long integer value representing the number of movies /// imported from the JSON file.</returns> public static async Task<long> BatchWriteItemsAsync( AmazonDynamoDBClient client, string movieFileName) { var movies = ImportMovies(movieFileName); if (movies is null) { Console.WriteLine("Couldn't find the JSON file with movie data."); return 0; } var context = new DynamoDBContext(client); var movieBatch = context.CreateBatchWrite<Movie>(); movieBatch.AddPutItems(movies); Console.WriteLine("Adding imported movies to the table."); await movieBatch.ExecuteAsync(); return movies.Count; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考BatchWriteItem中的。

场景

以下代码示例显示了操作流程:

  • 创建可保存电影数据的表。

  • 在表中加入单一电影,获取并更新此电影。

  • 向 JSON 示例文件的表中写入电影数据。

  • 查询在给定年份发行的电影。

  • 扫描在年份范围内发行的电影。

  • 删除表中的电影后再删除表。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

// This example application performs the following basic Amazon DynamoDB // functions: // // CreateTableAsync // PutItemAsync // UpdateItemAsync // BatchWriteItemAsync // GetItemAsync // DeleteItemAsync // Query // Scan // DeleteItemAsync // using Amazon.DynamoDBv2; using DynamoDB_Actions; public class DynamoDB_Basics { // Separator for the console display. private static readonly string SepBar = new string('-', 80); public static async Task Main() { var client = new AmazonDynamoDBClient(); var tableName = "movie_table"; // Relative path to moviedata.json in the local repository. var movieFileName = @"..\..\..\..\..\..\..\..\resources\sample_files\movies.json"; DisplayInstructions(); // Create a new table and wait for it to be active. Console.WriteLine($"Creating the new table: {tableName}"); var success = await DynamoDbMethods.CreateMovieTableAsync(client, tableName); if (success) { Console.WriteLine($"\nTable: {tableName} successfully created."); } else { Console.WriteLine($"\nCould not create {tableName}."); } WaitForEnter(); // Add a single new movie to the table. var newMovie = new Movie { Year = 2021, Title = "Spider-Man: No Way Home", }; success = await DynamoDbMethods.PutItemAsync(client, newMovie, tableName); if (success) { Console.WriteLine($"Added {newMovie.Title} to the table."); } else { Console.WriteLine("Could not add movie to table."); } WaitForEnter(); // Update the new movie by adding a plot and rank. var newInfo = new MovieInfo { Plot = "With Spider-Man's identity now revealed, Peter asks" + "Doctor Strange for help. When a spell goes wrong, dangerous" + "foes from other worlds start to appear, forcing Peter to" + "discover what it truly means to be Spider-Man.", Rank = 9, }; success = await DynamoDbMethods.UpdateItemAsync(client, newMovie, newInfo, tableName); if (success) { Console.WriteLine($"Successfully updated the movie: {newMovie.Title}"); } else { Console.WriteLine("Could not update the movie."); } WaitForEnter(); // Add a batch of movies to the DynamoDB table from a list of // movies in a JSON file. var itemCount = await DynamoDbMethods.BatchWriteItemsAsync(client, movieFileName); Console.WriteLine($"Added {itemCount} movies to the table."); WaitForEnter(); // Get a movie by key. (partition + sort) var lookupMovie = new Movie { Title = "Jurassic Park", Year = 1993, }; Console.WriteLine("Looking for the movie \"Jurassic Park\"."); var item = await DynamoDbMethods.GetItemAsync(client, lookupMovie, tableName); if (item.Count > 0) { DynamoDbMethods.DisplayItem(item); } else { Console.WriteLine($"Couldn't find {lookupMovie.Title}"); } WaitForEnter(); // Delete a movie. var movieToDelete = new Movie { Title = "The Town", Year = 2010, }; success = await DynamoDbMethods.DeleteItemAsync(client, tableName, movieToDelete); if (success) { Console.WriteLine($"Successfully deleted {movieToDelete.Title}."); } else { Console.WriteLine($"Could not delete {movieToDelete.Title}."); } WaitForEnter(); // Use Query to find all the movies released in 2010. int findYear = 2010; Console.WriteLine($"Movies released in {findYear}"); var queryCount = await DynamoDbMethods.QueryMoviesAsync(client, tableName, findYear); Console.WriteLine($"Found {queryCount} movies released in {findYear}"); WaitForEnter(); // Use Scan to get a list of movies from 2001 to 2011. int startYear = 2001; int endYear = 2011; var scanCount = await DynamoDbMethods.ScanTableAsync(client, tableName, startYear, endYear); Console.WriteLine($"Found {scanCount} movies released between {startYear} and {endYear}"); WaitForEnter(); // Delete the table. success = await DynamoDbMethods.DeleteTableAsync(client, tableName); if (success) { Console.WriteLine($"Successfully deleted {tableName}"); } else { Console.WriteLine($"Could not delete {tableName}"); } Console.WriteLine("The DynamoDB Basics example application is done."); WaitForEnter(); } /// <summary> /// Displays the description of the application on the console. /// </summary> private static void DisplayInstructions() { Console.Clear(); Console.WriteLine(); Console.Write(new string(' ', 28)); Console.WriteLine("DynamoDB Basics Example"); Console.WriteLine(SepBar); Console.WriteLine("This demo application shows the basics of using DynamoDB with the AWS SDK."); Console.WriteLine(SepBar); Console.WriteLine("The application does the following:"); Console.WriteLine("\t1. Creates a table with partition: year and sort:title."); Console.WriteLine("\t2. Adds a single movie to the table."); Console.WriteLine("\t3. Adds movies to the table from moviedata.json."); Console.WriteLine("\t4. Updates the rating and plot of the movie that was just added."); Console.WriteLine("\t5. Gets a movie using its key (partition + sort)."); Console.WriteLine("\t6. Deletes a movie."); Console.WriteLine("\t7. Uses QueryAsync to return all movies released in a given year."); Console.WriteLine("\t8. Uses ScanAsync to return all movies released within a range of years."); Console.WriteLine("\t9. Finally, it deletes the table that was just created."); WaitForEnter(); } /// <summary> /// Simple method to wait for the Enter key to be pressed. /// </summary> private static void WaitForEnter() { Console.WriteLine("\nPress <Enter> to continue."); Console.WriteLine(SepBar); _ = Console.ReadLine(); } }

创建一个包含影片数据的表。

/// <summary> /// Creates a new Amazon DynamoDB table and then waits for the new /// table to become active. /// </summary> /// <param name="client">An initialized Amazon DynamoDB client object.</param> /// <param name="tableName">The name of the table to create.</param> /// <returns>A Boolean value indicating the success of the operation.</returns> public static async Task<bool> CreateMovieTableAsync(AmazonDynamoDBClient client, string tableName) { var response = await client.CreateTableAsync(new CreateTableRequest { TableName = tableName, AttributeDefinitions = new List<AttributeDefinition>() { new AttributeDefinition { AttributeName = "title", AttributeType = ScalarAttributeType.S, }, new AttributeDefinition { AttributeName = "year", AttributeType = ScalarAttributeType.N, }, }, KeySchema = new List<KeySchemaElement>() { new KeySchemaElement { AttributeName = "year", KeyType = KeyType.HASH, }, new KeySchemaElement { AttributeName = "title", KeyType = KeyType.RANGE, }, }, ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 5, WriteCapacityUnits = 5, }, }); // Wait until the table is ACTIVE and then report success. Console.Write("Waiting for table to become active..."); var request = new DescribeTableRequest { TableName = response.TableDescription.TableName, }; TableStatus status; int sleepDuration = 2000; do { System.Threading.Thread.Sleep(sleepDuration); var describeTableResponse = await client.DescribeTableAsync(request); status = describeTableResponse.Table.TableStatus; Console.Write("."); } while (status != "ACTIVE"); return status == TableStatus.ACTIVE; }

将单个影片添加到表中。

/// <summary> /// Adds a new item to the table. /// </summary> /// <param name="client">An initialized Amazon DynamoDB client object.</param> /// <param name="newMovie">A Movie object containing informtation for /// the movie to add to the table.</param> /// <param name="tableName">The name of the table where the item will be added.</param> /// <returns>A Boolean value that indicates the results of adding the item.</returns> public static async Task<bool> PutItemAsync(AmazonDynamoDBClient client, Movie newMovie, string tableName) { var item = new Dictionary<string, AttributeValue> { ["title"] = new AttributeValue { S = newMovie.Title }, ["year"] = new AttributeValue { N = newMovie.Year.ToString() }, }; var request = new PutItemRequest { TableName = tableName, Item = item, }; var response = await client.PutItemAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }

更新表中的单个项目。

/// <summary> /// Updates an existing item in the movies table. /// </summary> /// <param name="client">An initialized Amazon DynamoDB client object.</param> /// <param name="newMovie">A Movie object containing information for /// the movie to update.</param> /// <param name="newInfo">A MovieInfo object that contains the /// information that will be changed.</param> /// <param name="tableName">The name of the table that contains the movie.</param> /// <returns>A Boolean value that indicates the success of the operation.</returns> public static async Task<bool> UpdateItemAsync( AmazonDynamoDBClient client, Movie newMovie, MovieInfo newInfo, string tableName) { var key = new Dictionary<string, AttributeValue> { ["title"] = new AttributeValue { S = newMovie.Title }, ["year"] = new AttributeValue { N = newMovie.Year.ToString() }, }; var updates = new Dictionary<string, AttributeValueUpdate> { ["info.plot"] = new AttributeValueUpdate { Action = AttributeAction.PUT, Value = new AttributeValue { S = newInfo.Plot }, }, ["info.rating"] = new AttributeValueUpdate { Action = AttributeAction.PUT, Value = new AttributeValue { N = newInfo.Rank.ToString() }, }, }; var request = new UpdateItemRequest { AttributeUpdates = updates, Key = key, TableName = tableName, }; var response = await client.UpdateItemAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }

从影片表中检索单个项目。

/// <summary> /// Gets information about an existing movie from the table. /// </summary> /// <param name="client">An initialized Amazon DynamoDB client object.</param> /// <param name="newMovie">A Movie object containing information about /// the movie to retrieve.</param> /// <param name="tableName">The name of the table containing the movie.</param> /// <returns>A Dictionary object containing information about the item /// retrieved.</returns> public static async Task<Dictionary<string, AttributeValue>> GetItemAsync(AmazonDynamoDBClient client, Movie newMovie, string tableName) { var key = new Dictionary<string, AttributeValue> { ["title"] = new AttributeValue { S = newMovie.Title }, ["year"] = new AttributeValue { N = newMovie.Year.ToString() }, }; var request = new GetItemRequest { Key = key, TableName = tableName, }; var response = await client.GetItemAsync(request); return response.Item; }

将一批项目写入影片表。

/// <summary> /// Loads the contents of a JSON file into a list of movies to be /// added to the DynamoDB table. /// </summary> /// <param name="movieFileName">The full path to the JSON file.</param> /// <returns>A generic list of movie objects.</returns> public static List<Movie> ImportMovies(string movieFileName) { if (!File.Exists(movieFileName)) { return null; } using var sr = new StreamReader(movieFileName); string json = sr.ReadToEnd(); var allMovies = JsonSerializer.Deserialize<List<Movie>>( json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); // Now return the first 250 entries. return allMovies.GetRange(0, 250); } /// <summary> /// Writes 250 items to the movie table. /// </summary> /// <param name="client">The initialized DynamoDB client object.</param> /// <param name="movieFileName">A string containing the full path to /// the JSON file containing movie data.</param> /// <returns>A long integer value representing the number of movies /// imported from the JSON file.</returns> public static async Task<long> BatchWriteItemsAsync( AmazonDynamoDBClient client, string movieFileName) { var movies = ImportMovies(movieFileName); if (movies is null) { Console.WriteLine("Couldn't find the JSON file with movie data."); return 0; } var context = new DynamoDBContext(client); var movieBatch = context.CreateBatchWrite<Movie>(); movieBatch.AddPutItems(movies); Console.WriteLine("Adding imported movies to the table."); await movieBatch.ExecuteAsync(); return movies.Count; }

从表中删除单个项目。

/// <summary> /// Deletes a single item from a DynamoDB table. /// </summary> /// <param name="client">The initialized DynamoDB client object.</param> /// <param name="tableName">The name of the table from which the item /// will be deleted.</param> /// <param name="movieToDelete">A movie object containing the title and /// year of the movie to delete.</param> /// <returns>A Boolean value indicating the success or failure of the /// delete operation.</returns> public static async Task<bool> DeleteItemAsync( AmazonDynamoDBClient client, string tableName, Movie movieToDelete) { var key = new Dictionary<string, AttributeValue> { ["title"] = new AttributeValue { S = movieToDelete.Title }, ["year"] = new AttributeValue { N = movieToDelete.Year.ToString() }, }; var request = new DeleteItemRequest { TableName = tableName, Key = key, }; var response = await client.DeleteItemAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }

查询表中是否有特定年份发行的影片。

/// <summary> /// Queries the table for movies released in a particular year and /// then displays the information for the movies returned. /// </summary> /// <param name="client">The initialized DynamoDB client object.</param> /// <param name="tableName">The name of the table to query.</param> /// <param name="year">The release year for which we want to /// view movies.</param> /// <returns>The number of movies that match the query.</returns> public static async Task<int> QueryMoviesAsync(AmazonDynamoDBClient client, string tableName, int year) { var movieTable = Table.LoadTable(client, tableName); var filter = new QueryFilter("year", QueryOperator.Equal, year); Console.WriteLine("\nFind movies released in: {year}:"); var config = new QueryOperationConfig() { Limit = 10, // 10 items per page. Select = SelectValues.SpecificAttributes, AttributesToGet = new List<string> { "title", "year", }, ConsistentRead = true, Filter = filter, }; // Value used to track how many movies match the // supplied criteria. var moviesFound = 0; Search search = movieTable.Query(config); do { var movieList = await search.GetNextSetAsync(); moviesFound += movieList.Count; foreach (var movie in movieList) { DisplayDocument(movie); } } while (!search.IsDone); return moviesFound; }

扫描此表以查找几年内发行的影片。

public static async Task<int> ScanTableAsync( AmazonDynamoDBClient client, string tableName, int startYear, int endYear) { var request = new ScanRequest { TableName = tableName, ExpressionAttributeNames = new Dictionary<string, string> { { "#yr", "year" }, }, ExpressionAttributeValues = new Dictionary<string, AttributeValue> { { ":y_a", new AttributeValue { N = startYear.ToString() } }, { ":y_z", new AttributeValue { N = endYear.ToString() } }, }, FilterExpression = "#yr between :y_a and :y_z", ProjectionExpression = "#yr, title, info.actors[0], info.directors, info.running_time_secs", Limit = 10 // Set a limit to demonstrate using the LastEvaluatedKey. }; // Keep track of how many movies were found. int foundCount = 0; var response = new ScanResponse(); do { response = await client.ScanAsync(request); foundCount += response.Items.Count; response.Items.ForEach(i => DisplayItem(i)); request.ExclusiveStartKey = response.LastEvaluatedKey; } while (response.LastEvaluatedKey.Count > 0); return foundCount; }

删除影片表。

public static async Task<bool> DeleteTableAsync(AmazonDynamoDBClient client, string tableName) { var request = new DeleteTableRequest { TableName = tableName, }; var response = await client.DeleteTableAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Table {response.TableDescription.TableName} successfully deleted."); return true; } else { Console.WriteLine("Could not delete table."); return false; } }

以下代码示例演示了操作流程:

  • 通过运行多个 SELECT 语句来获取一批项目。

  • 通过运行多个 INSERT 语句来添加一批项目。

  • 通过运行多个 UPDATE 语句来更新一批项目。

  • 通过运行多个 DELETE 语句来删除一批项目。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

// Before you run this example, download 'movies.json' from // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Js.02.html, // and put it in the same folder as the example. // Separator for the console display. var SepBar = new string('-', 80); const string tableName = "movie_table"; const string movieFileName = "moviedata.json"; DisplayInstructions(); // Create the table and wait for it to be active. Console.WriteLine($"Creating the movie table: {tableName}"); var success = await DynamoDBMethods.CreateMovieTableAsync(tableName); if (success) { Console.WriteLine($"Successfully created table: {tableName}."); } WaitForEnter(); // Add movie information to the table from moviedata.json. See the // instructions at the top of this file to download the JSON file. Console.WriteLine($"Inserting movies into the new table. Please wait..."); success = await PartiQLBatchMethods.InsertMovies(tableName, movieFileName); if (success) { Console.WriteLine("Movies successfully added to the table."); } else { Console.WriteLine("Movies could not be added to the table."); } WaitForEnter(); // Update multiple movies by using the BatchExecute statement. var title1 = "Star Wars"; var year1 = 1977; var title2 = "Wizard of Oz"; var year2 = 1939; Console.WriteLine($"Updating two movies with producer information: {title1} and {title2}."); success = await PartiQLBatchMethods.GetBatch(tableName, title1, title2, year1, year2); if (success) { Console.WriteLine($"Successfully retrieved {title1} and {title2}."); } else { Console.WriteLine("Select statement failed."); } WaitForEnter(); // Update multiple movies by using the BatchExecute statement. var producer1 = "LucasFilm"; var producer2 = "MGM"; Console.WriteLine($"Updating two movies with producer information: {title1} and {title2}."); success = await PartiQLBatchMethods.UpdateBatch(tableName, producer1, title1, year1, producer2, title2, year2); if (success) { Console.WriteLine($"Successfully updated {title1} and {title2}."); } else { Console.WriteLine("Update failed."); } WaitForEnter(); // Delete multiple movies by using the BatchExecute statement. Console.WriteLine($"Now we will delete {title1} and {title2} from the table."); success = await PartiQLBatchMethods.DeleteBatch(tableName, title1, year1, title2, year2); if (success) { Console.WriteLine($"Deleted {title1} and {title2}"); } else { Console.WriteLine($"could not delete {title1} or {title2}"); } WaitForEnter(); // DNow that the PartiQL Batch scenario is complete, delete the movie table. success = await DynamoDBMethods.DeleteTableAsync(tableName); if (success) { Console.WriteLine($"Successfully deleted {tableName}"); } else { Console.WriteLine($"Could not delete {tableName}"); } /// <summary> /// Displays the description of the application on the console. /// </summary> void DisplayInstructions() { Console.Clear(); Console.WriteLine(); Console.Write(new string(' ', 24)); Console.WriteLine("DynamoDB PartiQL Basics Example"); Console.WriteLine(SepBar); Console.WriteLine("This demo application shows the basics of using Amazon DynamoDB with the AWS SDK for"); Console.WriteLine(".NET version 3.7 and .NET 6."); Console.WriteLine(SepBar); Console.WriteLine("Creates a table by using the CreateTable method."); Console.WriteLine("Gets multiple movies by using a PartiQL SELECT statement."); Console.WriteLine("Updates multiple movies by using the ExecuteBatch method."); Console.WriteLine("Deletes multiple movies by using a PartiQL DELETE statement."); Console.WriteLine("Cleans up the resources created for the demo by deleting the table."); Console.WriteLine(SepBar); WaitForEnter(); } /// <summary> /// Simple method to wait for the <Enter> key to be pressed. /// </summary> void WaitForEnter() { Console.WriteLine("\nPress <Enter> to continue."); Console.Write(SepBar); _ = Console.ReadLine(); } /// <summary> /// Gets movies from the movie table by /// using an Amazon DynamoDB PartiQL SELECT statement. /// </summary> /// <param name="tableName">The name of the table.</param> /// <param name="title1">The title of the first movie.</param> /// <param name="title2">The title of the second movie.</param> /// <param name="year1">The year of the first movie.</param> /// <param name="year2">The year of the second movie.</param> /// <returns>True if successful.</returns> public static async Task<bool> GetBatch( string tableName, string title1, string title2, int year1, int year2) { var getBatch = $"SELECT FROM {tableName} WHERE title = ? AND year = ?"; var statements = new List<BatchStatementRequest> { new BatchStatementRequest { Statement = getBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = title1 }, new AttributeValue { N = year1.ToString() }, }, }, new BatchStatementRequest { Statement = getBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = title2 }, new AttributeValue { N = year2.ToString() }, }, } }; var response = await Client.BatchExecuteStatementAsync(new BatchExecuteStatementRequest { Statements = statements, }); if (response.Responses.Count > 0) { response.Responses.ForEach(r => { Console.WriteLine($"{r.Item["title"]}\t{r.Item["year"]}"); }); return true; } else { Console.WriteLine($"Couldn't find either {title1} or {title2}."); return false; } } /// <summary> /// Inserts movies imported from a JSON file into the movie table by /// using an Amazon DynamoDB PartiQL INSERT statement. /// </summary> /// <param name="tableName">The name of the table into which the movie /// information will be inserted.</param> /// <param name="movieFileName">The name of the JSON file that contains /// movie information.</param> /// <returns>A Boolean value that indicates the success or failure of /// the insert operation.</returns> public static async Task<bool> InsertMovies(string tableName, string movieFileName) { // Get the list of movies from the JSON file. var movies = ImportMovies(movieFileName); var success = false; if (movies is not null) { // Insert the movies in a batch using PartiQL. Because the // batch can contain a maximum of 25 items, insert 25 movies // at a time. string insertBatch = $"INSERT INTO {tableName} VALUE {{'title': ?, 'year': ?}}"; var statements = new List<BatchStatementRequest>(); try { for (var indexOffset = 0; indexOffset < 250; indexOffset += 25) { for (var i = indexOffset; i < indexOffset + 25; i++) { statements.Add(new BatchStatementRequest { Statement = insertBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = movies[i].Title }, new AttributeValue { N = movies[i].Year.ToString() }, }, }); } var response = await Client.BatchExecuteStatementAsync(new BatchExecuteStatementRequest { Statements = statements, }); // Wait between batches for movies to be successfully added. System.Threading.Thread.Sleep(3000); success = response.HttpStatusCode == System.Net.HttpStatusCode.OK; // Clear the list of statements for the next batch. statements.Clear(); } } catch (AmazonDynamoDBException ex) { Console.WriteLine(ex.Message); } } return success; } /// <summary> /// Loads the contents of a JSON file into a list of movies to be /// added to the DynamoDB table. /// </summary> /// <param name="movieFileName">The full path to the JSON file.</param> /// <returns>A generic list of movie objects.</returns> public static List<Movie> ImportMovies(string movieFileName) { if (!File.Exists(movieFileName)) { return null!; } using var sr = new StreamReader(movieFileName); string json = sr.ReadToEnd(); var allMovies = JsonConvert.DeserializeObject<List<Movie>>(json); if (allMovies is not null) { // Return the first 250 entries. return allMovies.GetRange(0, 250); } else { return null!; } } /// <summary> /// Updates information for multiple movies. /// </summary> /// <param name="tableName">The name of the table containing the /// movies to be updated.</param> /// <param name="producer1">The producer name for the first movie /// to update.</param> /// <param name="title1">The title of the first movie.</param> /// <param name="year1">The year that the first movie was released.</param> /// <param name="producer2">The producer name for the second /// movie to update.</param> /// <param name="title2">The title of the second movie.</param> /// <param name="year2">The year that the second movie was released.</param> /// <returns>A Boolean value that indicates the success of the update.</returns> public static async Task<bool> UpdateBatch( string tableName, string producer1, string title1, int year1, string producer2, string title2, int year2) { string updateBatch = $"UPDATE {tableName} SET Producer=? WHERE title = ? AND year = ?"; var statements = new List<BatchStatementRequest> { new BatchStatementRequest { Statement = updateBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = producer1 }, new AttributeValue { S = title1 }, new AttributeValue { N = year1.ToString() }, }, }, new BatchStatementRequest { Statement = updateBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = producer2 }, new AttributeValue { S = title2 }, new AttributeValue { N = year2.ToString() }, }, } }; var response = await Client.BatchExecuteStatementAsync(new BatchExecuteStatementRequest { Statements = statements, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <summary> /// Deletes multiple movies using a PartiQL BatchExecuteAsync /// statement. /// </summary> /// <param name="tableName">The name of the table containing the /// moves that will be deleted.</param> /// <param name="title1">The title of the first movie.</param> /// <param name="year1">The year the first movie was released.</param> /// <param name="title2">The title of the second movie.</param> /// <param name="year2">The year the second movie was released.</param> /// <returns>A Boolean value indicating the success of the operation.</returns> public static async Task<bool> DeleteBatch( string tableName, string title1, int year1, string title2, int year2) { string updateBatch = $"DELETE FROM {tableName} WHERE title = ? AND year = ?"; var statements = new List<BatchStatementRequest> { new BatchStatementRequest { Statement = updateBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = title1 }, new AttributeValue { N = year1.ToString() }, }, }, new BatchStatementRequest { Statement = updateBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = title2 }, new AttributeValue { N = year2.ToString() }, }, } }; var response = await Client.BatchExecuteStatementAsync(new BatchExecuteStatementRequest { Statements = statements, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }

以下代码示例演示了操作流程:

  • 通过运行 SELECT 语句来获取项目。

  • 通过运行 INSERT 语句来添加项目。

  • 通过运行 UPDATE 语句来更新项目。

  • 通过运行 DELETE 语句来删除项目。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

namespace PartiQL_Basics_Scenario { public class PartiQLMethods { private static readonly AmazonDynamoDBClient Client = new AmazonDynamoDBClient(); /// <summary> /// Inserts movies imported from a JSON file into the movie table by /// using an Amazon DynamoDB PartiQL INSERT statement. /// </summary> /// <param name="tableName">The name of the table where the movie /// information will be inserted.</param> /// <param name="movieFileName">The name of the JSON file that contains /// movie information.</param> /// <returns>A Boolean value that indicates the success or failure of /// the insert operation.</returns> public static async Task<bool> InsertMovies(string tableName, string movieFileName) { // Get the list of movies from the JSON file. var movies = ImportMovies(movieFileName); var success = false; if (movies is not null) { // Insert the movies in a batch using PartiQL. Because the // batch can contain a maximum of 25 items, insert 25 movies // at a time. string insertBatch = $"INSERT INTO {tableName} VALUE {{'title': ?, 'year': ?}}"; var statements = new List<BatchStatementRequest>(); try { for (var indexOffset = 0; indexOffset < 250; indexOffset += 25) { for (var i = indexOffset; i < indexOffset + 25; i++) { statements.Add(new BatchStatementRequest { Statement = insertBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = movies[i].Title }, new AttributeValue { N = movies[i].Year.ToString() }, }, }); } var response = await Client.BatchExecuteStatementAsync(new BatchExecuteStatementRequest { Statements = statements, }); // Wait between batches for movies to be successfully added. System.Threading.Thread.Sleep(3000); success = response.HttpStatusCode == System.Net.HttpStatusCode.OK; // Clear the list of statements for the next batch. statements.Clear(); } } catch (AmazonDynamoDBException ex) { Console.WriteLine(ex.Message); } } return success; } /// <summary> /// Loads the contents of a JSON file into a list of movies to be /// added to the DynamoDB table. /// </summary> /// <param name="movieFileName">The full path to the JSON file.</param> /// <returns>A generic list of movie objects.</returns> public static List<Movie> ImportMovies(string movieFileName) { if (!File.Exists(movieFileName)) { return null!; } using var sr = new StreamReader(movieFileName); string json = sr.ReadToEnd(); var allMovies = JsonConvert.DeserializeObject<List<Movie>>(json); if (allMovies is not null) { // Return the first 250 entries. return allMovies.GetRange(0, 250); } else { return null!; } } /// <summary> /// Uses a PartiQL SELECT statement to retrieve a single movie from the /// movie database. /// </summary> /// <param name="tableName">The name of the movie table.</param> /// <param name="movieTitle">The title of the movie to retrieve.</param> /// <returns>A list of movie data. If no movie matches the supplied /// title, the list is empty.</returns> public static async Task<List<Dictionary<string, AttributeValue>>> GetSingleMovie(string tableName, string movieTitle) { string selectSingle = $"SELECT * FROM {tableName} WHERE title = ?"; var parameters = new List<AttributeValue> { new AttributeValue { S = movieTitle }, }; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = selectSingle, Parameters = parameters, }); return response.Items; } /// <summary> /// Retrieve multiple movies by year using a SELECT statement. /// </summary> /// <param name="tableName">The name of the movie table.</param> /// <param name="year">The year the movies were released.</param> /// <returns></returns> public static async Task<List<Dictionary<string, AttributeValue>>> GetMovies(string tableName, int year) { string selectSingle = $"SELECT * FROM {tableName} WHERE year = ?"; var parameters = new List<AttributeValue> { new AttributeValue { N = year.ToString() }, }; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = selectSingle, Parameters = parameters, }); return response.Items; } /// <summary> /// Inserts a single movie into the movies table. /// </summary> /// <param name="tableName">The name of the table.</param> /// <param name="movieTitle">The title of the movie to insert.</param> /// <param name="year">The year that the movie was released.</param> /// <returns>A Boolean value that indicates the success or failure of /// the INSERT operation.</returns> public static async Task<bool> InsertSingleMovie(string tableName, string movieTitle, int year) { string insertBatch = $"INSERT INTO {tableName} VALUE {{'title': ?, 'year': ?}}"; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = insertBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = movieTitle }, new AttributeValue { N = year.ToString() }, }, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <summary> /// Updates a single movie in the table, adding information for the /// producer. /// </summary> /// <param name="tableName">the name of the table.</param> /// <param name="producer">The name of the producer.</param> /// <param name="movieTitle">The movie title.</param> /// <param name="year">The year the movie was released.</param> /// <returns>A Boolean value that indicates the success of the /// UPDATE operation.</returns> public static async Task<bool> UpdateSingleMovie(string tableName, string producer, string movieTitle, int year) { string insertSingle = $"UPDATE {tableName} SET Producer=? WHERE title = ? AND year = ?"; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = insertSingle, Parameters = new List<AttributeValue> { new AttributeValue { S = producer }, new AttributeValue { S = movieTitle }, new AttributeValue { N = year.ToString() }, }, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <summary> /// Deletes a single movie from the table. /// </summary> /// <param name="tableName">The name of the table.</param> /// <param name="movieTitle">The title of the movie to delete.</param> /// <param name="year">The year that the movie was released.</param> /// <returns>A Boolean value that indicates the success of the /// DELETE operation.</returns> public static async Task<bool> DeleteSingleMovie(string tableName, string movieTitle, int year) { var deleteSingle = $"DELETE FROM {tableName} WHERE title = ? AND year = ?"; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = deleteSingle, Parameters = new List<AttributeValue> { new AttributeValue { S = movieTitle }, new AttributeValue { N = year.ToString() }, }, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <summary> /// Displays the list of movies returned from a database query. /// </summary> /// <param name="items">The list of movie information to display.</param> private static void DisplayMovies(List<Dictionary<string, AttributeValue>> items) { if (items.Count > 0) { Console.WriteLine($"Found {items.Count} movies."); items.ForEach(item => Console.WriteLine($"{item["year"].N}\t{item["title"].S}")); } else { Console.WriteLine($"Didn't find a movie that matched the supplied criteria."); } } } } /// <summary> /// Uses a PartiQL SELECT statement to retrieve a single movie from the /// movie database. /// </summary> /// <param name="tableName">The name of the movie table.</param> /// <param name="movieTitle">The title of the movie to retrieve.</param> /// <returns>A list of movie data. If no movie matches the supplied /// title, the list is empty.</returns> public static async Task<List<Dictionary<string, AttributeValue>>> GetSingleMovie(string tableName, string movieTitle) { string selectSingle = $"SELECT * FROM {tableName} WHERE title = ?"; var parameters = new List<AttributeValue> { new AttributeValue { S = movieTitle }, }; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = selectSingle, Parameters = parameters, }); return response.Items; } /// <summary> /// Inserts a single movie into the movies table. /// </summary> /// <param name="tableName">The name of the table.</param> /// <param name="movieTitle">The title of the movie to insert.</param> /// <param name="year">The year that the movie was released.</param> /// <returns>A Boolean value that indicates the success or failure of /// the INSERT operation.</returns> public static async Task<bool> InsertSingleMovie(string tableName, string movieTitle, int year) { string insertBatch = $"INSERT INTO {tableName} VALUE {{'title': ?, 'year': ?}}"; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = insertBatch, Parameters = new List<AttributeValue> { new AttributeValue { S = movieTitle }, new AttributeValue { N = year.ToString() }, }, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <summary> /// Updates a single movie in the table, adding information for the /// producer. /// </summary> /// <param name="tableName">the name of the table.</param> /// <param name="producer">The name of the producer.</param> /// <param name="movieTitle">The movie title.</param> /// <param name="year">The year the movie was released.</param> /// <returns>A Boolean value that indicates the success of the /// UPDATE operation.</returns> public static async Task<bool> UpdateSingleMovie(string tableName, string producer, string movieTitle, int year) { string insertSingle = $"UPDATE {tableName} SET Producer=? WHERE title = ? AND year = ?"; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = insertSingle, Parameters = new List<AttributeValue> { new AttributeValue { S = producer }, new AttributeValue { S = movieTitle }, new AttributeValue { N = year.ToString() }, }, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <summary> /// Deletes a single movie from the table. /// </summary> /// <param name="tableName">The name of the table.</param> /// <param name="movieTitle">The title of the movie to delete.</param> /// <param name="year">The year that the movie was released.</param> /// <returns>A Boolean value that indicates the success of the /// DELETE operation.</returns> public static async Task<bool> DeleteSingleMovie(string tableName, string movieTitle, int year) { var deleteSingle = $"DELETE FROM {tableName} WHERE title = ? AND year = ?"; var response = await Client.ExecuteStatementAsync(new ExecuteStatementRequest { Statement = deleteSingle, Parameters = new List<AttributeValue> { new AttributeValue { S = movieTitle }, new AttributeValue { N = year.ToString() }, }, }); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考ExecuteStatement中的。

以下代码示例显示如何使用 DynamoDB 的文档模型以及 Amazon SDK 来执行创建、读取、更新和删除 (CRUD) 及批处理操作。

有关更多信息,请参阅文档模型

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

使用文档模型执行 CRUD 操作。

/// <summary> /// Performs CRUD operations on an Amazon DynamoDB table. /// </summary> public class MidlevelItemCRUD { public static async Task Main() { var tableName = "ProductCatalog"; var sampleBookId = 555; var client = new AmazonDynamoDBClient(); var productCatalog = LoadTable(client, tableName); await CreateBookItem(productCatalog, sampleBookId); RetrieveBook(productCatalog, sampleBookId); // Couple of sample updates. UpdateMultipleAttributes(productCatalog, sampleBookId); UpdateBookPriceConditionally(productCatalog, sampleBookId); // Delete. await DeleteBook(productCatalog, sampleBookId); } /// <summary> /// Loads the contents of a DynamoDB table. /// </summary> /// <param name="client">An initialized DynamoDB client object.</param> /// <param name="tableName">The name of the table to load.</param> /// <returns>A DynamoDB table object.</returns> public static Table LoadTable(IAmazonDynamoDB client, string tableName) { Table productCatalog = Table.LoadTable(client, tableName); return productCatalog; } /// <summary> /// Creates an example book item and adds it to the DynamoDB table /// ProductCatalog. /// </summary> /// <param name="productCatalog">A DynamoDB table object.</param> /// <param name="sampleBookId">An integer value representing the book's ID.</param> public static async Task CreateBookItem(Table productCatalog, int sampleBookId) { Console.WriteLine("\n*** Executing CreateBookItem() ***"); var book = new Document { ["Id"] = sampleBookId, ["Title"] = "Book " + sampleBookId, ["Price"] = 19.99, ["ISBN"] = "111-1111111111", ["Authors"] = new List<string> { "Author 1", "Author 2", "Author 3" }, ["PageCount"] = 500, ["Dimensions"] = "8.5x11x.5", ["InPublication"] = new DynamoDBBool(true), ["InStock"] = new DynamoDBBool(false), ["QuantityOnHand"] = 0, }; // Adds the book to the ProductCatalog table. await productCatalog.PutItemAsync(book); } /// <summary> /// Retrieves an item, a book, from the DynamoDB ProductCatalog table. /// </summary> /// <param name="productCatalog">A DynamoDB table object.</param> /// <param name="sampleBookId">An integer value representing the book's ID.</param> public static async void RetrieveBook( Table productCatalog, int sampleBookId) { Console.WriteLine("\n*** Executing RetrieveBook() ***"); // Optional configuration. var config = new GetItemOperationConfig { AttributesToGet = new List<string> { "Id", "ISBN", "Title", "Authors", "Price" }, ConsistentRead = true, }; Document document = await productCatalog.GetItemAsync(sampleBookId, config); Console.WriteLine("RetrieveBook: Printing book retrieved..."); PrintDocument(document); } /// <summary> /// Updates multiple attributes for a book and writes the changes to the /// DynamoDB table ProductCatalog. /// </summary> /// <param name="productCatalog">A DynamoDB table object.</param> /// <param name="sampleBookId">An integer value representing the book's ID.</param> public static async void UpdateMultipleAttributes( Table productCatalog, int sampleBookId) { Console.WriteLine("\nUpdating multiple attributes...."); int partitionKey = sampleBookId; var book = new Document { ["Id"] = partitionKey, // List of attribute updates. // The following replaces the existing authors list. ["Authors"] = new List<string> { "Author x", "Author y" }, ["newAttribute"] = "New Value", ["ISBN"] = null, // Remove it. }; // Optional parameters. var config = new UpdateItemOperationConfig { // Gets updated item in response. ReturnValues = ReturnValues.AllNewAttributes, }; Document updatedBook = await productCatalog.UpdateItemAsync(book, config); Console.WriteLine("UpdateMultipleAttributes: Printing item after updates ..."); PrintDocument(updatedBook); } /// <summary> /// Updates a book item if it meets the specified criteria. /// </summary> /// <param name="productCatalog">A DynamoDB table object.</param> /// <param name="sampleBookId">An integer value representing the book's ID.</param> public static async void UpdateBookPriceConditionally( Table productCatalog, int sampleBookId) { Console.WriteLine("\n*** Executing UpdateBookPriceConditionally() ***"); int partitionKey = sampleBookId; var book = new Document { ["Id"] = partitionKey, ["Price"] = 29.99, }; // For conditional price update, creating a condition expression. var expr = new Expression { ExpressionStatement = "Price = :val", }; expr.ExpressionAttributeValues[":val"] = 19.00; // Optional parameters. var config = new UpdateItemOperationConfig { ConditionalExpression = expr, ReturnValues = ReturnValues.AllNewAttributes, }; Document updatedBook = await productCatalog.UpdateItemAsync(book, config); Console.WriteLine("UpdateBookPriceConditionally: Printing item whose price was conditionally updated"); PrintDocument(updatedBook); } /// <summary> /// Deletes the book with the supplied Id value from the DynamoDB table /// ProductCatalog. /// </summary> /// <param name="productCatalog">A DynamoDB table object.</param> /// <param name="sampleBookId">An integer value representing the book's ID.</param> public static async Task DeleteBook( Table productCatalog, int sampleBookId) { Console.WriteLine("\n*** Executing DeleteBook() ***"); // Optional configuration. var config = new DeleteItemOperationConfig { // Returns the deleted item. ReturnValues = ReturnValues.AllOldAttributes, }; Document document = await productCatalog.DeleteItemAsync(sampleBookId, config); Console.WriteLine("DeleteBook: Printing deleted just deleted..."); PrintDocument(document); } /// <summary> /// Prints the information for the supplied DynamoDB document. /// </summary> /// <param name="updatedDocument">A DynamoDB document object.</param> public static void PrintDocument(Document updatedDocument) { if (updatedDocument is null) { return; } foreach (var attribute in updatedDocument.GetAttributeNames()) { string stringValue = null; var value = updatedDocument[attribute]; if (value is null) { continue; } if (value is Primitive) { stringValue = value.AsPrimitive().Value.ToString(); } else if (value is PrimitiveList) { stringValue = string.Join(",", (from primitive in value.AsPrimitiveList().Entries select primitive.Value).ToArray()); } Console.WriteLine($"{attribute} - {stringValue}", attribute, stringValue); } } }

使用文档模型执行批量写入操作。

/// <summary> /// Shows how to use mid-level Amazon DynamoDB API calls to perform batch /// operations. /// </summary> public class MidLevelBatchWriteItem { public static async Task Main() { IAmazonDynamoDB client = new AmazonDynamoDBClient(); await SingleTableBatchWrite(client); await MultiTableBatchWrite(client); } /// <summary> /// Perform a batch operation on a single DynamoDB table. /// </summary> /// <param name="client">An initialized DynamoDB object.</param> public static async Task SingleTableBatchWrite(IAmazonDynamoDB client) { Table productCatalog = Table.LoadTable(client, "ProductCatalog"); var batchWrite = productCatalog.CreateBatchWrite(); var book1 = new Document { ["Id"] = 902, ["Title"] = "My book1 in batch write using .NET helper classes", ["ISBN"] = "902-11-11-1111", ["Price"] = 10, ["ProductCategory"] = "Book", ["Authors"] = new List<string> { "Author 1", "Author 2", "Author 3" }, ["Dimensions"] = "8.5x11x.5", ["InStock"] = new DynamoDBBool(true), ["QuantityOnHand"] = new DynamoDBNull(), // Quantity is unknown at this time. }; batchWrite.AddDocumentToPut(book1); // Specify delete item using overload that takes PK. batchWrite.AddKeyToDelete(12345); Console.WriteLine("Performing batch write in SingleTableBatchWrite()"); await batchWrite.ExecuteAsync(); } /// <summary> /// Perform a batch operation involving multiple DynamoDB tables. /// </summary> /// <param name="client">An initialized DynamoDB client object.</param> public static async Task MultiTableBatchWrite(IAmazonDynamoDB client) { // Specify item to add in the Forum table. Table forum = Table.LoadTable(client, "Forum"); var forumBatchWrite = forum.CreateBatchWrite(); var forum1 = new Document { ["Name"] = "Test BatchWrite Forum", ["Threads"] = 0, }; forumBatchWrite.AddDocumentToPut(forum1); // Specify item to add in the Thread table. Table thread = Table.LoadTable(client, "Thread"); var threadBatchWrite = thread.CreateBatchWrite(); var thread1 = new Document { ["ForumName"] = "S3 forum", ["Subject"] = "My sample question", ["Message"] = "Message text", ["KeywordTags"] = new List<string> { "S3", "Bucket" }, }; threadBatchWrite.AddDocumentToPut(thread1); // Specify item to delete from the Thread table. threadBatchWrite.AddKeyToDelete("someForumName", "someSubject"); // Create multi-table batch. var superBatch = new MultiTableDocumentBatchWrite(); superBatch.AddBatch(forumBatchWrite); superBatch.AddBatch(threadBatchWrite); Console.WriteLine("Performing batch write in MultiTableBatchWrite()"); // Execute the batch. await superBatch.ExecuteAsync(); } }

使用文档模型扫描表。

/// <summary> /// Shows how to use mid-level Amazon DynamoDB API calls to scan a DynamoDB /// table for values. /// </summary> public class MidLevelScanOnly { public static async Task Main() { IAmazonDynamoDB client = new AmazonDynamoDBClient(); Table productCatalogTable = Table.LoadTable(client, "ProductCatalog"); await FindProductsWithNegativePrice(productCatalogTable); await FindProductsWithNegativePriceWithConfig(productCatalogTable); } /// <summary> /// Retrieves any products that have a negative price in a DynamoDB table. /// </summary> /// <param name="productCatalogTable">A DynamoDB table object.</param> public static async Task FindProductsWithNegativePrice( Table productCatalogTable) { // Assume there is a price error. So we scan to find items priced < 0. var scanFilter = new ScanFilter(); scanFilter.AddCondition("Price", ScanOperator.LessThan, 0); Search search = productCatalogTable.Scan(scanFilter); do { var documentList = await search.GetNextSetAsync(); Console.WriteLine("\nFindProductsWithNegativePrice: printing ............"); foreach (var document in documentList) { PrintDocument(document); } } while (!search.IsDone); } /// <summary> /// Finds any items in the ProductCatalog table using a DynamoDB /// configuration object. /// </summary> /// <param name="productCatalogTable">A DynamoDB table object.</param> public static async Task FindProductsWithNegativePriceWithConfig( Table productCatalogTable) { // Assume there is a price error. So we scan to find items priced < 0. var scanFilter = new ScanFilter(); scanFilter.AddCondition("Price", ScanOperator.LessThan, 0); var config = new ScanOperationConfig() { Filter = scanFilter, Select = SelectValues.SpecificAttributes, AttributesToGet = new List<string> { "Title", "Id" }, }; Search search = productCatalogTable.Scan(config); do { var documentList = await search.GetNextSetAsync(); Console.WriteLine("\nFindProductsWithNegativePriceWithConfig: printing ............"); foreach (var document in documentList) { PrintDocument(document); } } while (!search.IsDone); } /// <summary> /// Displays the details of the passed DynamoDB document object on the /// console. /// </summary> /// <param name="document">A DynamoDB document object.</param> public static void PrintDocument(Document document) { Console.WriteLine(); foreach (var attribute in document.GetAttributeNames()) { string stringValue = null; var value = document[attribute]; if (value is Primitive) { stringValue = value.AsPrimitive().Value.ToString(); } else if (value is PrimitiveList) { stringValue = string.Join(",", (from primitive in value.AsPrimitiveList().Entries select primitive.Value).ToArray()); } Console.WriteLine($"{attribute} - {stringValue}"); } } }

使用文档模型查询和扫描表。

/// <summary> /// Shows how to perform mid-level query procedures on an Amazon DynamoDB /// table. /// </summary> public class MidLevelQueryAndScan { public static async Task Main() { IAmazonDynamoDB client = new AmazonDynamoDBClient(); // Query examples. Table replyTable = Table.LoadTable(client, "Reply"); string forumName = "Amazon DynamoDB"; string threadSubject = "DynamoDB Thread 2"; await FindRepliesInLast15Days(replyTable); await FindRepliesInLast15DaysWithConfig(replyTable, forumName, threadSubject); await FindRepliesPostedWithinTimePeriod(replyTable, forumName, threadSubject); // Get Example. Table productCatalogTable = Table.LoadTable(client, "ProductCatalog"); int productId = 101; await GetProduct(productCatalogTable, productId); } /// <summary> /// Retrieves information about a product from the DynamoDB table /// ProductCatalog based on the product ID and displays the information /// on the console. /// </summary> /// <param name="tableName">The name of the table from which to retrieve /// product information.</param> /// <param name="productId">The ID of the product to retrieve.</param> public static async Task GetProduct(Table tableName, int productId) { Console.WriteLine("*** Executing GetProduct() ***"); Document productDocument = await tableName.GetItemAsync(productId); if (productDocument != null) { PrintDocument(productDocument); } else { Console.WriteLine("Error: product " + productId + " does not exist"); } } /// <summary> /// Retrieves replies from the passed DynamoDB table object. /// </summary> /// <param name="table">The table we want to query.</param> public static async Task FindRepliesInLast15Days( Table table) { DateTime twoWeeksAgoDate = DateTime.UtcNow - TimeSpan.FromDays(15); var filter = new QueryFilter("Id", QueryOperator.Equal, "Id"); filter.AddCondition("ReplyDateTime", QueryOperator.GreaterThan, twoWeeksAgoDate); // Use Query overloads that take the minimum required query parameters. Search search = table.Query(filter); do { var documentSet = await search.GetNextSetAsync(); Console.WriteLine("\nFindRepliesInLast15Days: printing ............"); foreach (var document in documentSet) { PrintDocument(document); } } while (!search.IsDone); } /// <summary> /// Retrieve replies made during a specific time period. /// </summary> /// <param name="table">The table we want to query.</param> /// <param name="forumName">The name of the forum that we're interested in.</param> /// <param name="threadSubject">The subject of the thread, which we are /// searching for replies.</param> public static async Task FindRepliesPostedWithinTimePeriod( Table table, string forumName, string threadSubject) { DateTime startDate = DateTime.UtcNow.Subtract(new TimeSpan(21, 0, 0, 0)); DateTime endDate = DateTime.UtcNow.Subtract(new TimeSpan(1, 0, 0, 0)); var filter = new QueryFilter("Id", QueryOperator.Equal, forumName + "#" + threadSubject); filter.AddCondition("ReplyDateTime", QueryOperator.Between, startDate, endDate); var config = new QueryOperationConfig() { Limit = 2, // 2 items/page. Select = SelectValues.SpecificAttributes, AttributesToGet = new List<string> { "Message", "ReplyDateTime", "PostedBy", }, ConsistentRead = true, Filter = filter, }; Search search = table.Query(config); do { var documentList = await search.GetNextSetAsync(); Console.WriteLine("\nFindRepliesPostedWithinTimePeriod: printing replies posted within dates: {0} and {1} ............", startDate, endDate); foreach (var document in documentList) { PrintDocument(document); } } while (!search.IsDone); } /// <summary> /// Perform a query for replies made in the last 15 days using a DynamoDB /// QueryOperationConfig object. /// </summary> /// <param name="table">The table we want to query.</param> /// <param name="forumName">The name of the forum that we're interested in.</param> /// <param name="threadName">The bane of the thread that we are searching /// for replies.</param> public static async Task FindRepliesInLast15DaysWithConfig( Table table, string forumName, string threadName) { DateTime twoWeeksAgoDate = DateTime.UtcNow - TimeSpan.FromDays(15); var filter = new QueryFilter("Id", QueryOperator.Equal, forumName + "#" + threadName); filter.AddCondition("ReplyDateTime", QueryOperator.GreaterThan, twoWeeksAgoDate); var config = new QueryOperationConfig() { Filter = filter, // Optional parameters. Select = SelectValues.SpecificAttributes, AttributesToGet = new List<string> { "Message", "ReplyDateTime", "PostedBy", }, ConsistentRead = true, }; Search search = table.Query(config); do { var documentSet = await search.GetNextSetAsync(); Console.WriteLine("\nFindRepliesInLast15DaysWithConfig: printing ............"); foreach (var document in documentSet) { PrintDocument(document); } } while (!search.IsDone); } /// <summary> /// Displays the contents of the passed DynamoDB document on the console. /// </summary> /// <param name="document">A DynamoDB document to display.</param> public static void PrintDocument(Document document) { Console.WriteLine(); foreach (var attribute in document.GetAttributeNames()) { string stringValue = null; var value = document[attribute]; if (value is Primitive) { stringValue = value.AsPrimitive().Value.ToString(); } else if (value is PrimitiveList) { stringValue = string.Join(",", (from primitive in value.AsPrimitiveList().Entries select primitive.Value).ToArray()); } Console.WriteLine($"{attribute} - {stringValue}"); } } }

以下代码示例显示如何使用 DynamoDB 的对象持久化模型和 Amazon SDK 来执行创建、读取、更新和删除 (CRUD) 及批处理操作。

有关更多信息,请参阅对象持久化模型

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

使用高级对象持久化模型执行 CRUD 操作。

/// <summary> /// Shows how to perform high-level CRUD operations on an Amazon DynamoDB /// table. /// </summary> public class HighLevelItemCrud { public static async Task Main() { var client = new AmazonDynamoDBClient(); DynamoDBContext context = new DynamoDBContext(client); await PerformCRUDOperations(context); } public static async Task PerformCRUDOperations(IDynamoDBContext context) { int bookId = 1001; // Some unique value. Book myBook = new Book { Id = bookId, Title = "object persistence-AWS SDK for.NET SDK-Book 1001", Isbn = "111-1111111001", BookAuthors = new List<string> { "Author 1", "Author 2" }, }; // Save the book to the ProductCatalog table. await context.SaveAsync(myBook); // Retrieve the book from the ProductCatalog table. Book bookRetrieved = await context.LoadAsync<Book>(bookId); // Update some properties. bookRetrieved.Isbn = "222-2222221001"; // Update existing authors list with the following values. bookRetrieved.BookAuthors = new List<string> { " Author 1", "Author x" }; await context.SaveAsync(bookRetrieved); // Retrieve the updated book. This time, add the optional // ConsistentRead parameter using DynamoDBContextConfig object. await context.LoadAsync<Book>(bookId, new DynamoDBContextConfig { ConsistentRead = true, }); // Delete the book. await context.DeleteAsync<Book>(bookId); // Try to retrieve deleted book. It should return null. Book deletedBook = await context.LoadAsync<Book>(bookId, new DynamoDBContextConfig { ConsistentRead = true, }); if (deletedBook == null) { Console.WriteLine("Book is deleted"); } } }

使用高级对象持久化模型执行批量写入操作。

/// <summary> /// Performs high-level batch write operations to an Amazon DynamoDB table. /// This example was written using the AWS SDK for .NET version 3.7 and .NET /// Core 5.0. /// </summary> public class HighLevelBatchWriteItem { public static async Task SingleTableBatchWrite(IDynamoDBContext context) { Book book1 = new Book { Id = 902, InPublication = true, Isbn = "902-11-11-1111", PageCount = "100", Price = 10, ProductCategory = "Book", Title = "My book3 in batch write", }; Book book2 = new Book { Id = 903, InPublication = true, Isbn = "903-11-11-1111", PageCount = "200", Price = 10, ProductCategory = "Book", Title = "My book4 in batch write", }; var bookBatch = context.CreateBatchWrite<Book>(); bookBatch.AddPutItems(new List<Book> { book1, book2 }); Console.WriteLine("Adding two books to ProductCatalog table."); await bookBatch.ExecuteAsync(); } public static async Task MultiTableBatchWrite(IDynamoDBContext context) { // New Forum item. Forum newForum = new Forum { Name = "Test BatchWrite Forum", Threads = 0, }; var forumBatch = context.CreateBatchWrite<Forum>(); forumBatch.AddPutItem(newForum); // New Thread item. Thread newThread = new Thread { ForumName = "S3 forum", Subject = "My sample question", KeywordTags = new List<string> { "S3", "Bucket" }, Message = "Message text", }; DynamoDBOperationConfig config = new DynamoDBOperationConfig(); config.SkipVersionCheck = true; var threadBatch = context.CreateBatchWrite<Thread>(config); threadBatch.AddPutItem(newThread); threadBatch.AddDeleteKey("some partition key value", "some sort key value"); var superBatch = new MultiTableBatchWrite(forumBatch, threadBatch); Console.WriteLine("Performing batch write in MultiTableBatchWrite()."); await superBatch.ExecuteAsync(); } public static async Task Main() { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); DynamoDBContext context = new DynamoDBContext(client); await SingleTableBatchWrite(context); await MultiTableBatchWrite(context); } }

使用高级对象持久化模型将任意数据映射为表。

/// <summary> /// Shows how to map arbitrary data to an Amazon DynamoDB table. /// </summary> public class HighLevelMappingArbitraryData { /// <summary> /// Creates a book, adds it to the DynamoDB ProductCatalog table, retrieves /// the new book from the table, updates the dimensions and writes the /// changed item back to the table. /// </summary> /// <param name="context">The DynamoDB context object used to write and /// read data from the table.</param> public static async Task AddRetrieveUpdateBook(IDynamoDBContext context) { // Create a book. DimensionType myBookDimensions = new DimensionType() { Length = 8M, Height = 11M, Thickness = 0.5M, }; Book myBook = new Book { Id = 501, Title = "AWS SDK for .NET Object Persistence Model Handling Arbitrary Data", Isbn = "999-9999999999", BookAuthors = new List<string> { "Author 1", "Author 2" }, Dimensions = myBookDimensions, }; // Add the book to the DynamoDB table ProductCatalog. await context.SaveAsync(myBook); // Retrieve the book. Book bookRetrieved = await context.LoadAsync<Book>(501); // Update the book dimensions property. bookRetrieved.Dimensions.Height += 1; bookRetrieved.Dimensions.Length += 1; bookRetrieved.Dimensions.Thickness += 0.2M; // Write the changed item to the table. await context.SaveAsync(bookRetrieved); } public static async Task Main() { var client = new AmazonDynamoDBClient(); DynamoDBContext context = new DynamoDBContext(client); await AddRetrieveUpdateBook(context); } }

使用高级对象持久化模型查询和扫描表。

/// <summary> /// Shows how to perform high-level query and scan operations to Amazon /// DynamoDB tables. /// </summary> public class HighLevelQueryAndScan { public static async Task Main() { var client = new AmazonDynamoDBClient(); DynamoDBContext context = new DynamoDBContext(client); // Get an item. await GetBook(context, 101); // Sample forum and thread to test queries. string forumName = "Amazon DynamoDB"; string threadSubject = "DynamoDB Thread 1"; // Sample queries. await FindRepliesInLast15Days(context, forumName, threadSubject); await FindRepliesPostedWithinTimePeriod(context, forumName, threadSubject); // Scan table. await FindProductsPricedLessThanZero(context); } public static async Task GetBook(IDynamoDBContext context, int productId) { Book bookItem = await context.LoadAsync<Book>(productId); Console.WriteLine("\nGetBook: Printing result....."); Console.WriteLine($"Title: {bookItem.Title} \n ISBN:{bookItem.Isbn} \n No. of pages: {bookItem.PageCount}"); } /// <summary> /// Queries a DynamoDB table to find replies posted within the last 15 days. /// </summary> /// <param name="context">The DynamoDB context used to perform the query.</param> /// <param name="forumName">The name of the forum that we're interested in.</param> /// <param name="threadSubject">The thread object containing the query parameters.</param> public static async Task FindRepliesInLast15Days( IDynamoDBContext context, string forumName, string threadSubject) { string replyId = $"{forumName} #{threadSubject}"; DateTime twoWeeksAgoDate = DateTime.UtcNow - TimeSpan.FromDays(15); List<object> times = new List<object>(); times.Add(twoWeeksAgoDate); List<ScanCondition> scs = new List<ScanCondition>(); var sc = new ScanCondition("PostedBy", ScanOperator.GreaterThan, times.ToArray()); scs.Add(sc); var cfg = new DynamoDBOperationConfig { QueryFilter = scs, }; AsyncSearch<Reply> response = context.QueryAsync<Reply>(replyId, cfg); IEnumerable<Reply> latestReplies = await response.GetRemainingAsync(); Console.WriteLine("\nReplies in last 15 days:"); foreach (Reply r in latestReplies) { Console.WriteLine($"{r.Id}\t{r.PostedBy}\t{r.Message}\t{r.ReplyDateTime}"); } } /// <summary> /// Queries for replies posted within a specific time period. /// </summary> /// <param name="context">The DynamoDB context used to perform the query.</param> /// <param name="forumName">The name of the forum that we're interested in.</param> /// <param name="threadSubject">Information about the subject that we're /// interested in.</param> public static async Task FindRepliesPostedWithinTimePeriod( IDynamoDBContext context, string forumName, string threadSubject) { string forumId = forumName + "#" + threadSubject; Console.WriteLine("\nReplies posted within time period:"); DateTime startDate = DateTime.UtcNow - TimeSpan.FromDays(30); DateTime endDate = DateTime.UtcNow - TimeSpan.FromDays(1); List<object> times = new List<object>(); times.Add(startDate); times.Add(endDate); List<ScanCondition> scs = new List<ScanCondition>(); var sc = new ScanCondition("LastPostedBy", ScanOperator.Between, times.ToArray()); scs.Add(sc); var cfg = new DynamoDBOperationConfig { QueryFilter = scs, }; AsyncSearch<Reply> response = context.QueryAsync<Reply>(forumId, cfg); IEnumerable<Reply> repliesInAPeriod = await response.GetRemainingAsync(); foreach (Reply r in repliesInAPeriod) { Console.WriteLine("{r.Id}\t{r.PostedBy}\t{r.Message}\t{r.ReplyDateTime}"); } } /// <summary> /// Queries the DynamoDB ProductCatalog table for products costing less /// than zero. /// </summary> /// <param name="context">The DynamoDB context object used to perform the /// query.</param> public static async Task FindProductsPricedLessThanZero(IDynamoDBContext context) { int price = 0; List<ScanCondition> scs = new List<ScanCondition>(); var sc1 = new ScanCondition("Price", ScanOperator.LessThan, price); var sc2 = new ScanCondition("ProductCategory", ScanOperator.Equal, "Book"); scs.Add(sc1); scs.Add(sc2); AsyncSearch<Book> response = context.ScanAsync<Book>(scs); IEnumerable<Book> itemsWithWrongPrice = await response.GetRemainingAsync(); Console.WriteLine("\nFindProductsPricedLessThanZero: Printing result....."); foreach (Book r in itemsWithWrongPrice) { Console.WriteLine($"{r.Id}\t{r.Title}\t{r.Price}\t{r.Isbn}"); } } }