| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
The following C# code example declares Book, Forum, Thread, and Reply classes and maps them to the Amazon DynamoDB tables using the object persistence model attributes.
The code example then uses the DynamoDBContext to illustrate
the following batch write operations.
BatchWrite object to put and delete book items from the ProductCatalog
table.
MultiTableBatchWrite object to put and delete items from the Forum and the Thread
tables.
For more information about the tables used in this example, see Example Tables and Data in Amazon DynamoDB. For step-by-step instructions to test the following sample, see Using the AWS SDK for .NET.
using System;
using System.Collections.Generic;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.Runtime;
using Amazon.SecurityToken;
namespace Amazon.DynamoDBv2.Documentation
{
class ORMTest
{
static void Main(string[] args)
{
try
{
var client = CreateClient();
DynamoDBContext context = new DynamoDBContext(client);
SingleTableBatchWrite(context);
MultiTableBatchWrite(context);
}
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(DynamoDBContext 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("Performing batch write in SingleTableBatchWrite().");
bookBatch.Execute();
}
private static void MultiTableBatchWrite(DynamoDBContext context)
{
// 1. New Forum item.
Forum newForum = new Forum
{
Name = "Test BatchWrite Forum",
Threads = 0
};
var forumBatch = context.CreateBatchWrite<Forum>();
forumBatch.AddPutItem(newForum);
// 2. 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 hash attr", "some range attr");
var superBatch = new MultiTableBatchWrite(forumBatch, threadBatch);
Console.WriteLine("Performing batch write in MultiTableBatchWrite().");
superBatch.Execute();
}
private static AmazonDynamoDBClient CreateClient()
{
var userCredentials = new EnvironmentAWSCredentials();
var stsClient = new AmazonSecurityTokenServiceClient(userCredentials);
var sessionCredentials = new RefreshingSessionAWSCredentials(stsClient);
var config = new AmazonDynamoDBConfig();
config.ServiceURL = System.Configuration.ConfigurationManager.AppSettings["ServiceURL"];
var client = new AmazonDynamoDBClient(sessionCredentials, config);
return client;
}
}
[DynamoDBTable("Reply")]
public class Reply
{
[DynamoDBHashKey] // Hash key.
public string Id { get; set; }
[DynamoDBRangeKey] // Range key.
public DateTime ReplyDateTime { get; set; }
// Properties included implicitly.
public string Message { get; set; }
// Explicit property mapping with object persistence model attributes.
[DynamoDBProperty("LastPostedBy")]
public string PostedBy { get; set; }
// Property to store version number for optimistic locking.
[DynamoDBVersion]
public int? Version { get; set; }
}
[DynamoDBTable("Thread")]
public class Thread
{
// PK mapping.
[DynamoDBHashKey]
public string ForumName { get; set; }
[DynamoDBRangeKey]
public String Subject { get; set; }
// Implicit mapping.
public string Message { get; set; }
public string LastPostedBy { get; set; }
public int Views { get; set; }
public int Replies { get; set; }
public bool Answered { get; set; }
public DateTime LastPostedDateTime { get; set; }
// Explicit mapping (property and table attribute names are different.
[DynamoDBProperty("Tags")]
public List<string> KeywordTags { get; set; }
// Property to store version number for optimistic locking.
[DynamoDBVersion]
public int? Version { get; set; }
}
[DynamoDBTable("Forum")]
public class Forum
{
[DynamoDBHashKey]
public string Name { get; set; }
// All the following properties are explicitly mapped,
// only to show how to provide mapping.
[DynamoDBProperty]
public int Threads { get; set; }
[DynamoDBProperty]
public int Views { get; set; }
[DynamoDBProperty]
public string LastPostBy { get; set; }
[DynamoDBProperty]
public DateTime LastPostDateTime { get; set; }
[DynamoDBProperty]
public int Messages { get; set; }
}
[DynamoDBTable("ProductCatalog")]
public class Book
{
[DynamoDBHashKey] // Hash key.
public int Id { get; set; }
public string Title { get; set; }
public string ISBN { get; set; }
public int Price { get; set; }
public string PageCount { get; set; }
public string ProductCategory { get; set; }
public bool InPublication { get; set; }
}
}