翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
を使用した DynamoDB の例 AWS SDK for .NET
次のコード例は、DynamoDB AWS SDK for .NET で を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。
「基本」は、重要なオペレーションをサービス内で実行する方法を示すコード例です。
アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。
「シナリオ」は、1 つのサービス内から、または他の AWS のサービスと組み合わせて複数の関数を呼び出し、特定のタスクを実行する方法を示すコード例です。
各例には、完全なソースコードへのリンクが含まれています。ここでは、コンテキストでコードを設定および実行する方法の手順を確認できます。
開始方法
次のコード例は、DynamoDB の使用を開始する方法を示しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 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 詳細については、「 リファレンスListTables」の「」を参照してください。 AWS SDK for .NET API
-
基本
次のコードサンプルは、以下の操作方法を示しています。
映画データを保持できるテーブルを作成する。
テーブルに 1 つの映画を入れ、取得して更新する。
サンプルJSONファイルからテーブルに映画データを書き込みます。
特定の年にリリースされた映画を照会する。
何年もの間にリリースされた映画をスキャンする。
テーブルからムービーを削除し、テーブルを削除します。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 // 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; }
1 つのムービーをテーブルに追加します。
/// <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; }
テーブルの 1 つの項目を更新します。
/// <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; }
ムービーテーブルから 1 つの項目を取得します。
/// <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; }
テーブルから 1 つの項目を削除します。
/// <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; } }
-
API 詳細については、 AWS SDK for .NET APIリファレンスの以下のトピックを参照してください。
-
アクション
次の例は、BatchExecuteStatement
を使用する方法を説明しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 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; }
-
API 詳細については、 リファレンスBatchExecuteStatementの「」を参照してください。 AWS SDK for .NET API
-
次の例は、BatchGetItem
を使用する方法を説明しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 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 詳細については、 リファレンスBatchGetItemの「」を参照してください。 AWS SDK for .NET API
-
次の例は、BatchWriteItem
を使用する方法を説明しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 項目のバッチをムービーテーブルに書き込みます。
/// <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 詳細については、 リファレンスBatchWriteItemの「」を参照してください。 AWS SDK for .NET API
-
次のコード例は、CreateTable
を使用する方法を示しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 /// <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 詳細については、「 リファレンスCreateTable」の「」を参照してください。 AWS SDK for .NET API
-
次のコード例は、DeleteItem
を使用する方法を示しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 /// <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 詳細については、 リファレンスDeleteItemの「」を参照してください。 AWS SDK for .NET API
-
次の例は、DeleteTable
を使用する方法を説明しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 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 詳細については、 リファレンスDeleteTableの「」を参照してください。 AWS SDK for .NET API
-
次のコード例は、DescribeTable
を使用する方法を示しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 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 詳細については、 リファレンスDescribeTableの「」を参照してください。 AWS SDK for .NET API
-
次のコード例は、ExecuteStatement
を使用する方法を示しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 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 ステートメントを使用して、1 つの映画を削除します。
/// <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 詳細については、 リファレンスExecuteStatementの「」を参照してください。 AWS SDK for .NET API
-
次のコード例は、GetItem
を使用する方法を示しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 /// <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 詳細については、 リファレンスGetItemの「」を参照してください。 AWS SDK for .NET API
-
次のコード例は、ListTables
を使用する方法を示しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 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 詳細については、 リファレンスListTablesの「」を参照してください。 AWS SDK for .NET API
-
次の例は、PutItem
を使用する方法を説明しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 /// <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 詳細については、 リファレンスPutItemの「」を参照してください。 AWS SDK for .NET API
-
次の例は、Query
を使用する方法を説明しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 /// <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 詳細については、「 リファレンス」の「クエリ」を参照してください。 AWS SDK for .NET API
-
次の例は、Scan
を使用する方法を説明しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 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 詳細については、 AWS SDK for .NET APIリファレンスの「スキャン」を参照してください。
-
次のコード例は、UpdateItem
を使用する方法を示しています。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 /// <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 詳細については、 リファレンスUpdateItemの「」を参照してください。 AWS SDK for .NET API
-
シナリオ
次のコード例では、ユーザーがラベルを使用して写真を管理できるサーバーレスアプリケーションを作成する方法について示しています。
- AWS SDK for .NET
-
Amazon Rekognition を使用して画像内のラベルを検出し、保存して後で取得できるようにする写真アセット管理アプリケーションの開発方法を示します。
完全なソースコードとセットアップと実行の手順については、 の詳細な例を参照してください GitHub
。 この例のソースについて詳しくは、AWS コミュニティ
でブログ投稿を参照してください。 この例で使用されているサービス
API ゲートウェイ
DynamoDB
Lambda
Amazon Rekognition
Amazon S3
Amazon SNS
次のコード例は、Amazon DynamoDB テーブル内の作業項目を追跡し、Amazon Simple Email Service (Amazon SES) を使用してレポートを送信するウェブアプリケーションを作成する方法を示しています。
- AWS SDK for .NET
-
Amazon DynamoDB NETを使用してAPI、DynamoDB の作業データを追跡する動的ウェブアプリケーションを作成する方法を示します。
完全なソースコードとセットアップと実行の手順については、 の詳細な例を参照してくださいGitHub
。 この例で使用されているサービス
DynamoDB
Amazon SES
次のコードサンプルは、以下の操作方法を示しています。
複数のSELECTステートメントを実行して、項目のバッチを取得します。
複数のINSERTステートメントを実行して、項目のバッチを追加します。
複数のUPDATEステートメントを実行して、項目のバッチを更新します。
複数のDELETEステートメントを実行して、項目のバッチを削除します。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 // 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; }
-
API 詳細については、 リファレンスBatchExecuteStatementの「」を参照してください。 AWS SDK for .NET API
-
次のコードサンプルは、以下の操作方法を示しています。
SELECT ステートメントを実行して項目を取得します。
INSERT ステートメントを実行して項目を追加します。
UPDATE ステートメントを実行して項目を更新します。
DELETE ステートメントを実行して項目を削除します。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 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 詳細については、 リファレンスExecuteStatementの「」を参照してください。 AWS SDK for .NET API
-
次のコード例は、DynamoDB と のドキュメントモデルを使用して、作成、読み取り、更新、削除 (CRUD) およびバッチオペレーションを実行する方法を示しています AWS SDK。
詳細については、「ドキュメントモデル」を参照してください。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 ドキュメントモデルを使用して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 および のオブジェクト永続性モデルを使用して、作成、読み取り、更新、削除 (CRUD) およびバッチオペレーションを実行する方法を示しています AWS SDK。
詳細については、「オブジェクト永続性モデル」を参照してください。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 高レベルのオブジェクト永続性モデルを使用して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}"); } } }
サーバーレスサンプル
次のコード例は、DynamoDB ストリームからレコードを受信することでトリガーされるイベントを受信する Lambda 関数を実装する方法を示しています。関数は DynamoDB ペイロードを取得し、レコードの内容をログ記録します。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。サーバーレスサンプル
リポジトリで完全な例を検索し、設定および実行の方法を確認してください。 を使用して Lambda で DynamoDB イベントを消費するNET。
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 using System.Text.Json; using System.Text; using Amazon.Lambda.Core; using Amazon.Lambda.DynamoDBEvents; // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] namespace AWSLambda_DDB; public class Function { public void FunctionHandler(DynamoDBEvent dynamoEvent, ILambdaContext context) { context.Logger.LogInformation($"Beginning to process {dynamoEvent.Records.Count} records..."); foreach (var record in dynamoEvent.Records) { context.Logger.LogInformation($"Event ID: {record.EventID}"); context.Logger.LogInformation($"Event Name: {record.EventName}"); context.Logger.LogInformation(JsonSerializer.Serialize(record)); } context.Logger.LogInformation("Stream processing complete."); } }
次のコード例は、DynamoDB ストリームからイベントを受信する Lambda 関数に部分的なバッチレスポンスを実装する方法を示しています。この関数は、レスポンスとしてバッチアイテムの失敗を報告し、対象のメッセージを後で再試行するよう Lambda に伝えます。
- AWS SDK for .NET
-
注記
の詳細については、「」を参照してください GitHub。サーバーレスサンプル
リポジトリで完全な例を検索し、設定および実行の方法を確認してください。 を使用して Lambda で DynamoDB バッチ項目の障害を報告するNET。
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 using System.Text.Json; using System.Text; using Amazon.Lambda.Core; using Amazon.Lambda.DynamoDBEvents; // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] namespace AWSLambda_DDB; public class Function { public StreamsEventResponse FunctionHandler(DynamoDBEvent dynamoEvent, ILambdaContext context) { context.Logger.LogInformation($"Beginning to process {dynamoEvent.Records.Count} records..."); List<StreamsEventResponse.BatchItemFailure> batchItemFailures = new List<StreamsEventResponse.BatchItemFailure>(); StreamsEventResponse streamsEventResponse = new StreamsEventResponse(); foreach (var record in dynamoEvent.Records) { try { var sequenceNumber = record.Dynamodb.SequenceNumber; context.Logger.LogInformation(sequenceNumber); } catch (Exception ex) { context.Logger.LogError(ex.Message); batchItemFailures.Add(new StreamsEventResponse.BatchItemFailure() { ItemIdentifier = record.Dynamodb.SequenceNumber }); } } if (batchItemFailures.Count > 0) { streamsEventResponse.BatchItemFailures = batchItemFailures; } context.Logger.LogInformation("Stream processing complete."); return streamsEventResponse; } }