예: AWS SDK for .NET 문서 모델 API를 사용하는 일괄 작업 - Amazon DynamoDB

예: AWS SDK for .NET 문서 모델 API를 사용하는 일괄 작업

예: AWS SDK for .NET 문서 모델을 사용하는 일괄 쓰기

다음 C# 코드 예는 단일 테이블 및 여러 테이블 일괄 쓰기 작업을 보여 줍니다. 이 예에서는 다음과 같은 작업을 수행합니다.

  • 단일 테이블 배치 쓰기를 보여줍니다. ProductCatalog 테이블에 두 개의 항목을 추가합니다.

  • 다중 테이블 배치 쓰기를 보여줍니다. Forum 테이블과 Thread 테이블 모두에 항목을 추가하고 Thread 테이블에서 항목을 삭제합니다.

DynamoDB에서 테이블 생성 및 코드 예시에 대한 데이터 로드의 단계를 따랐다면 이미 ProductCatalog, ForumThread 테이블은 생성되어 있을 것입니다. 이러한 샘플 테이블은 프로그래밍 방식으로 생성할 수도 있습니다. 자세한 내용은 AWS SDK for .NET를 사용한 예시 테이블 생성 및 데이터 업로드 단원을 참조하십시오. 다음 예제를 테스트하기 위한 단계별 지침은 .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(); } } }