Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Amazon.DynamoDBv2;
- using Amazon.DynamoDBv2.DataModel;
- using Amazon.DynamoDBv2.DocumentModel;
- using Amazon.Runtime;
- using static System.Console;
- using static ConsoleApp2.Ext;
- namespace ConsoleApp2
- {
- public static class Ext
- {
- public static IEnumerable<object> L<T>(T one) => new[] {(object) one};
- public static IEnumerable<object> L<T>(T one, T two) => new[] {(object) one, two};
- }
- public static class HighLevelQueryAndScan
- {
- private static readonly AmazonDynamoDBClient Client = new AmazonDynamoDBClient();
- public static async Task Main()
- {
- try
- {
- var context = new DynamoDBContext(Client, new DynamoDBContextConfig()
- {
- TableNamePrefix = "je-qa29-"
- });
- // Get item.
- await GetBook(context, 101);
- // Sample forum and thread to test queries.
- var forumName = "Amazon DynamoDB";
- var threadSubject = "DynamoDB Thread 1";
- // Sample queries.
- await FindRepliesInLast15Days(context, forumName, threadSubject);
- await FindRepliesPostedWithinTimePeriod(context, forumName, threadSubject);
- // Scan table.
- await FindProductsPricedLessThanZero(context);
- WriteLine("To continue, press Enter");
- ReadLine();
- }
- catch (Exception e)
- {
- WriteLine(e.Message);
- }
- }
- private static async Task GetBook(IDynamoDBContext context, int productId)
- {
- var productItem = await context.LoadAsync<Product>(productId);
- WriteLine("\nGetBook: Printing result.....");
- WriteLine("Title: {0} \n No.Of threads:{1} \n No. of messages: {2}",
- productItem.Title, productItem.ISBN, productItem.PageCount);
- }
- private static async Task FindRepliesInLast15Days(IDynamoDBContext context,
- string forumName,
- string threadSubject)
- {
- var replyId = forumName + "#" + threadSubject;
- var twoWeeksAgoDate = DateTime.UtcNow - TimeSpan.FromDays(15);
- var latestReplies = await context.QueryAsync<Reply>(replyId, QueryOperator.GreaterThan, L(twoWeeksAgoDate)).GetRemainingAsync();
- WriteLine("\nFindRepliesInLast15Days: Printing result.....");
- foreach (var r in latestReplies)
- {
- WriteLine("{0}\t{1}\t{2}\t{3}", r.Id, r.PostedBy, r.Message, r.ReplyDateTime);
- }
- }
- private static async Task FindRepliesPostedWithinTimePeriod(IDynamoDBContext context,
- string forumName,
- string threadSubject)
- {
- var forumId = forumName + "#" + threadSubject;
- WriteLine("\nFindRepliesPostedWithinTimePeriod: Printing result.....");
- var startDate = DateTime.UtcNow - TimeSpan.FromDays(30);
- var endDate = DateTime.UtcNow - TimeSpan.FromDays(1);
- IEnumerable<Reply> repliesInAPeriod = await context
- .QueryAsync<Reply>(forumId, QueryOperator.Between, L(startDate, endDate))
- .GetRemainingAsync();
- foreach (var r in repliesInAPeriod)
- {
- WriteLine("{0}\t{1}\t{2}\t{3}", r.Id, r.PostedBy, r.Message, r.ReplyDateTime);
- }
- }
- private static async Task FindProductsPricedLessThanZero(IDynamoDBContext context)
- {
- const int price = 0;
- var products = await context.ScanAsync<Product>(new[]
- {
- new ScanCondition("Price", ScanOperator.LessThan, price),
- new ScanCondition("ProductCategory", ScanOperator.Equal, "Book")
- }
- ).GetRemainingAsync();
- WriteLine("\nFindProductsPricedLessThanZero: Printing result.....");
- foreach (var p in products)
- {
- WriteLine($"{p.Id}\t{p.Title}\t{p.Price}\t{p.ISBN}");
- }
- }
- }
- [DynamoDBTable("Reply")]
- public class Reply
- {
- [DynamoDBHashKey] //Partition key
- public string Id { get; set; }
- [DynamoDBRangeKey] //Sort 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] //Partition key
- public string ForumName { get; set; }
- [DynamoDBRangeKey] //Sort key
- public DateTime 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 Product
- {
- [DynamoDBHashKey] //Partition 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; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement