例: AWS SDK for .NET ドキュメントモデル API を使用したバッチオペレーション - Amazon DynamoDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

例: AWS SDK for .NET ドキュメントモデル API を使用したバッチオペレーション

例: AWS SDK for .NET ドキュメントモデルを使用したバッチ書き込み

次の C# コード例は、単一のテーブルと複数のテーブルに対するバッチ書き込みオペレーションを示しています。この例では次のタスクを実行しています。

  • 単一のテーブルバッチ書き込みを示します。ProductCatalog テーブルに 2 つの項目を追加します。

  • 複数のテーブルバッチ書き込みを示します。項目w2お Forum および Thread テーブルの両方に追加し、Thread から項目を削除します。

DynamoDB でのコード例用のテーブルの作成とデータのロード」のステップに従っていれば、ProductCatalogForum、および Thread テーブルは作成済みです。これらのサンプルテーブルは、プログラムで作成することもできます。詳細については、「を使用してサンプルテーブルを作成し、データをアップロードします。 AWS SDK for .NET」を参照してください。次の例をテストする step-by-step 手順については、「」を参照してください.NET コード例

using System; using System.Collections.Generic; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.DocumentModel; using Amazon.Runtime; namespace com.amazonaws.codesamples { class MidLevelBatchWriteItem { private static AmazonDynamoDBClient client = new AmazonDynamoDBClient(); static void Main(string[] args) { try { SingleTableBatchWrite(); MultiTableBatchWrite(); } catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); } catch (AmazonServiceException e) { Console.WriteLine(e.Message); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine("To continue, press Enter"); Console.ReadLine(); } private static void SingleTableBatchWrite() { Table productCatalog = Table.LoadTable(client, "ProductCatalog"); var batchWrite = productCatalog.CreateBatchWrite(); var book1 = new Document(); book1["Id"] = 902; book1["Title"] = "My book1 in batch write using .NET helper classes"; book1["ISBN"] = "902-11-11-1111"; book1["Price"] = 10; book1["ProductCategory"] = "Book"; book1["Authors"] = new List<string> { "Author 1", "Author 2", "Author 3" }; book1["Dimensions"] = "8.5x11x.5"; book1["InStock"] = new DynamoDBBool(true); book1["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()"); batchWrite.Execute(); } private static void MultiTableBatchWrite() { // 1. Specify item to add in the Forum table. Table forum = Table.LoadTable(client, "Forum"); var forumBatchWrite = forum.CreateBatchWrite(); var forum1 = new Document(); forum1["Name"] = "Test BatchWrite Forum"; forum1["Threads"] = 0; forumBatchWrite.AddDocumentToPut(forum1); // 2a. Specify item to add in the Thread table. Table thread = Table.LoadTable(client, "Thread"); var threadBatchWrite = thread.CreateBatchWrite(); var thread1 = new Document(); thread1["ForumName"] = "S3 forum"; thread1["Subject"] = "My sample question"; thread1["Message"] = "Message text"; thread1["KeywordTags"] = new List<string> { "S3", "Bucket" }; threadBatchWrite.AddDocumentToPut(thread1); // 2b. Specify item to delete from the Thread table. threadBatchWrite.AddKeyToDelete("someForumName", "someSubject"); // 3. Create multi-table batch. var superBatch = new MultiTableDocumentBatchWrite(); superBatch.AddBatch(forumBatchWrite); superBatch.AddBatch(threadBatchWrite); Console.WriteLine("Performing batch write in MultiTableBatchWrite()"); superBatch.Execute(); } } }