Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Amazon.DynamoDBv2;
  5. using Amazon.DynamoDBv2.DataModel;
  6. using Amazon.DynamoDBv2.DocumentModel;
  7. using Amazon.Runtime;
  8. using static System.Console;
  9. using static ConsoleApp2.Ext;
  10.  
  11. namespace ConsoleApp2
  12. {
  13. public static class Ext
  14. {
  15. public static IEnumerable<object> L<T>(T one) => new[] {(object) one};
  16. public static IEnumerable<object> L<T>(T one, T two) => new[] {(object) one, two};
  17. }
  18.  
  19. public static class HighLevelQueryAndScan
  20. {
  21. private static readonly AmazonDynamoDBClient Client = new AmazonDynamoDBClient();
  22.  
  23. public static async Task Main()
  24. {
  25. try
  26. {
  27. var context = new DynamoDBContext(Client, new DynamoDBContextConfig()
  28. {
  29. TableNamePrefix = "je-qa29-"
  30. });
  31.  
  32. // Get item.
  33. await GetBook(context, 101);
  34.  
  35. // Sample forum and thread to test queries.
  36. var forumName = "Amazon DynamoDB";
  37. var threadSubject = "DynamoDB Thread 1";
  38.  
  39. // Sample queries.
  40. await FindRepliesInLast15Days(context, forumName, threadSubject);
  41. await FindRepliesPostedWithinTimePeriod(context, forumName, threadSubject);
  42.  
  43. // Scan table.
  44. await FindProductsPricedLessThanZero(context);
  45. WriteLine("To continue, press Enter");
  46. ReadLine();
  47. }
  48. catch (Exception e)
  49. {
  50. WriteLine(e.Message);
  51. }
  52. }
  53.  
  54. private static async Task GetBook(IDynamoDBContext context, int productId)
  55. {
  56. var productItem = await context.LoadAsync<Product>(productId);
  57.  
  58. WriteLine("\nGetBook: Printing result.....");
  59. WriteLine("Title: {0} \n No.Of threads:{1} \n No. of messages: {2}",
  60. productItem.Title, productItem.ISBN, productItem.PageCount);
  61. }
  62.  
  63. private static async Task FindRepliesInLast15Days(IDynamoDBContext context,
  64. string forumName,
  65. string threadSubject)
  66. {
  67. var replyId = forumName + "#" + threadSubject;
  68. var twoWeeksAgoDate = DateTime.UtcNow - TimeSpan.FromDays(15);
  69. var latestReplies = await context.QueryAsync<Reply>(replyId, QueryOperator.GreaterThan, L(twoWeeksAgoDate)).GetRemainingAsync();
  70.  
  71. WriteLine("\nFindRepliesInLast15Days: Printing result.....");
  72.  
  73. foreach (var r in latestReplies)
  74. {
  75. WriteLine("{0}\t{1}\t{2}\t{3}", r.Id, r.PostedBy, r.Message, r.ReplyDateTime);
  76. }
  77. }
  78.  
  79. private static async Task FindRepliesPostedWithinTimePeriod(IDynamoDBContext context,
  80. string forumName,
  81. string threadSubject)
  82. {
  83. var forumId = forumName + "#" + threadSubject;
  84. WriteLine("\nFindRepliesPostedWithinTimePeriod: Printing result.....");
  85.  
  86. var startDate = DateTime.UtcNow - TimeSpan.FromDays(30);
  87. var endDate = DateTime.UtcNow - TimeSpan.FromDays(1);
  88.  
  89. IEnumerable<Reply> repliesInAPeriod = await context
  90. .QueryAsync<Reply>(forumId, QueryOperator.Between, L(startDate, endDate))
  91. .GetRemainingAsync();
  92.  
  93. foreach (var r in repliesInAPeriod)
  94. {
  95. WriteLine("{0}\t{1}\t{2}\t{3}", r.Id, r.PostedBy, r.Message, r.ReplyDateTime);
  96. }
  97. }
  98.  
  99. private static async Task FindProductsPricedLessThanZero(IDynamoDBContext context)
  100. {
  101. const int price = 0;
  102. var products = await context.ScanAsync<Product>(new[]
  103. {
  104. new ScanCondition("Price", ScanOperator.LessThan, price),
  105. new ScanCondition("ProductCategory", ScanOperator.Equal, "Book")
  106. }
  107. ).GetRemainingAsync();
  108.  
  109.  
  110. WriteLine("\nFindProductsPricedLessThanZero: Printing result.....");
  111.  
  112. foreach (var p in products)
  113. {
  114. WriteLine($"{p.Id}\t{p.Title}\t{p.Price}\t{p.ISBN}");
  115. }
  116. }
  117. }
  118.  
  119. [DynamoDBTable("Reply")]
  120. public class Reply
  121. {
  122. [DynamoDBHashKey] //Partition key
  123. public string Id { get; set; }
  124.  
  125. [DynamoDBRangeKey] //Sort key
  126. public DateTime ReplyDateTime { get; set; }
  127.  
  128. // Properties included implicitly.
  129. public string Message { get; set; }
  130.  
  131. // Explicit property mapping with object persistence model attributes.
  132. [DynamoDBProperty("LastPostedBy")] public string PostedBy { get; set; }
  133.  
  134. // Property to store version number for optimistic locking.
  135. [DynamoDBVersion] public int? Version { get; set; }
  136. }
  137.  
  138. [DynamoDBTable("Thread")]
  139. public class Thread
  140. {
  141. // PK mapping.
  142. [DynamoDBHashKey] //Partition key
  143. public string ForumName { get; set; }
  144.  
  145. [DynamoDBRangeKey] //Sort key
  146. public DateTime Subject { get; set; }
  147.  
  148. // Implicit mapping.
  149. public string Message { get; set; }
  150. public string LastPostedBy { get; set; }
  151. public int Views { get; set; }
  152. public int Replies { get; set; }
  153. public bool Answered { get; set; }
  154.  
  155. public DateTime LastPostedDateTime { get; set; }
  156.  
  157. // Explicit mapping (property and table attribute names are different.
  158. [DynamoDBProperty("Tags")] public List<string> KeywordTags { get; set; }
  159.  
  160. // Property to store version number for optimistic locking.
  161. [DynamoDBVersion] public int? Version { get; set; }
  162. }
  163.  
  164. [DynamoDBTable("Forum")]
  165. public class Forum
  166. {
  167. [DynamoDBHashKey] public string Name { get; set; }
  168.  
  169. // All the following properties are explicitly mapped,
  170. // only to show how to provide mapping.
  171. [DynamoDBProperty] public int Threads { get; set; }
  172. [DynamoDBProperty] public int Views { get; set; }
  173. [DynamoDBProperty] public string LastPostBy { get; set; }
  174. [DynamoDBProperty] public DateTime LastPostDateTime { get; set; }
  175. [DynamoDBProperty] public int Messages { get; set; }
  176. }
  177.  
  178. [DynamoDBTable("ProductCatalog")]
  179. public class Product
  180. {
  181. [DynamoDBHashKey] //Partition key
  182. public int Id { get; set; }
  183.  
  184. public string Title { get; set; }
  185. public string ISBN { get; set; }
  186. public int Price { get; set; }
  187. public string PageCount { get; set; }
  188. public string ProductCategory { get; set; }
  189. public bool InPublication { get; set; }
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement