Menu
Amazon DynamoDB
Developer Guide (API Version 2012-08-10)

Example: Query and Scan in DynamoDB Using the AWS SDK for .NET Object Persistence Model

The C# example in this section defines the following classes and maps them to the tables in DynamoDB. For more information about creating sample tables, see Creating Tables and Loading Sample Data.

  • Book class maps to ProductCatalog table

  • Forum, Thread, and Reply classes maps to the same name tables.

The example then executes the following query and scan operations using the DynamoDBContext.

  • Get a book by Id.

    The ProductCatalog table has Id as its primary key. It does not have a sort key as part of its primary key. Therefore, you cannot query the table. You can get an item using its Id value.

  • Execute the following queries against the Reply table (the Reply table's primary key is composed of Id and ReplyDateTime attributes. The ReplyDateTime is a sort key. Therefore, you can query this table).

    • Find replies to a forum thread posted in the last 15 days.

    • Find replies to a forum thread posted in a specific date range.

  • Scan ProductCatalog table to find books whose price is less than zero.

    For performance reasons, you should use a query instead of a scan operation. However, there are times you might need to scan a table. Suppose there was a data entry error and one of the book prices is set to less than 0. This example scans the ProductCategory table to find book items (ProductCategory is book) at price of less than 0.

For instructions to create a working sample, see .NET Code Samples.

using System;
using System.Collections.Generic;
using System.Configuration;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Runtime;

namespace com.amazonaws.codesamples
{
    class HighLevelQueryAndScan
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();

        static void Main(string[] args)
        {
            try
            {
                DynamoDBContext context = new DynamoDBContext(client);
                // Get item.
                GetBook(context, 101);

                // Sample forum and thread to test queries.
                string forumName = "Amazon DynamoDB";
                string threadSubject = "DynamoDB Thread 1";
                // Sample queries.
                FindRepliesInLast15Days(context, forumName, threadSubject);
                FindRepliesPostedWithinTimePeriod(context, forumName, threadSubject);

                // Scan table.
                FindProductsPricedLessThanZero(context);
                Console.WriteLine("To continue, press Enter");
                Console.ReadLine();
            }
            catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); }
            catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
            catch (Exception e) { Console.WriteLine(e.Message); }
        }

        private static void GetBook(DynamoDBContext context, int productId)
        {
            Book bookItem = context.Load<Book>(productId);

            Console.WriteLine("\nGetBook: Printing result.....");
            Console.WriteLine("Title: {0} \n No.Of threads:{1} \n No. of messages: {2}",
                                bookItem.Title, bookItem.ISBN, bookItem.PageCount);
        }

        private static void FindRepliesInLast15Days(DynamoDBContext context,
                                                    string forumName,
                                                    string threadSubject)
        {
            string replyId = forumName + "#" + threadSubject;
            DateTime twoWeeksAgoDate = DateTime.UtcNow - TimeSpan.FromDays(15);
            IEnumerable<Reply> latestReplies =
              context.Query<Reply>(replyId, QueryOperator.GreaterThan, twoWeeksAgoDate);
            Console.WriteLine("\nFindRepliesInLast15Days: Printing result.....");
            foreach (Reply r in latestReplies)
                Console.WriteLine("{0}\t{1}\t{2}\t{3}", r.Id, r.PostedBy, r.Message, r.ReplyDateTime);
        }

        private static void FindRepliesPostedWithinTimePeriod(DynamoDBContext context,
                                                              string forumName,
                                                              string threadSubject)
        {
            string forumId = forumName + "#" + threadSubject;
            Console.WriteLine("\nFindRepliesPostedWithinTimePeriod: Printing result.....");

            DateTime startDate = DateTime.UtcNow - TimeSpan.FromDays(30);
            DateTime endDate = DateTime.UtcNow - TimeSpan.FromDays(1);

            IEnumerable<Reply> repliesInAPeriod = context.Query<Reply>(forumId,
                                                                      QueryOperator.Between, startDate, endDate);
            foreach (Reply r in repliesInAPeriod)
                Console.WriteLine("{0}\t{1}\t{2}\t{3}", r.Id, r.PostedBy, r.Message, r.ReplyDateTime);
        }

        private static void FindProductsPricedLessThanZero(DynamoDBContext context)
        {
            int price = 0;
            IEnumerable<Book> itemsWithWrongPrice = context.Scan<Book>(
                  new ScanCondition("Price", ScanOperator.LessThan, price),
                  new ScanCondition("ProductCategory", ScanOperator.Equal, "Book")
                  );
            Console.WriteLine("\nFindProductsPricedLessThanZero: Printing result.....");
            foreach (Book r in itemsWithWrongPrice)
                Console.WriteLine("{0}\t{1}\t{2}\t{3}", r.Id, r.Title, r.Price, r.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 Book
    {
        [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; }
    }
}