Пример #1
0
        static void Main(string[] args)
        {
            // Get an AmazonDynamoDBClient for the local DynamoDB database
            AmazonDynamoDBClient client = GetLocalClient();

            // Get a Table object for the table that you created in Step 1
            Table table = GetTableObject(client, "Movies");

            if (table == null)
            {
                PauseForDebugWindow();
                return;
            }

            /*-----------------------------------------------------------------------
             *  4.1.1:  Call Table.Query to initiate a query for all movies with
             *          year == 1985, using an empty filter expression.
             *-----------------------------------------------------------------------*/
            Search search;

            try
            {
                search = table.Query(1985, new Expression());
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n Error: 1985 query failed because: " + ex.Message);
                PauseForDebugWindow();
                return;
            }

            // Display the titles of the movies returned by this query
            List <Document> docList = new List <Document>();

            Console.WriteLine("\n All movies released in 1985:" +
                              "\n-----------------------------------------------");
            do
            {
                try { docList = search.GetNextSet(); }
                catch (Exception ex)
                {
                    Console.WriteLine("\n Error: Search.GetNextStep failed because: " + ex.Message);
                    break;
                }
                foreach (var doc in docList)
                {
                    Console.WriteLine("    " + doc["title"]);
                }
            } while (!search.IsDone);


            /*-----------------------------------------------------------------------
             *  4.1.2a:  Call Table.Query to initiate a query for all movies where
             *           year equals 1992 AND title is between "B" and "Hzzz",
             *           returning the lead actor and genres of each.
             *-----------------------------------------------------------------------*/
            Primitive            y_1992 = new Primitive("1992", true);
            QueryOperationConfig config = new QueryOperationConfig();

            config.Filter = new QueryFilter();
            config.Filter.AddCondition("year", QueryOperator.Equal, new DynamoDBEntry[] { 1992 });
            config.Filter.AddCondition("title", QueryOperator.Between, new DynamoDBEntry[] { "B", "Hzz" });
            config.AttributesToGet = new List <string> {
                "title", "info"
            };
            config.Select = SelectValues.SpecificAttributes;

            try
            {
                search = table.Query(config);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n Error: 1992 query failed because: " + ex.Message);
                PauseForDebugWindow();
                return;
            }

            // Display the movie information returned by this query
            Console.WriteLine("\n\n Movies released in 1992 with titles between \"B\" and \"Hzz\" (Document Model):" +
                              "\n-----------------------------------------------------------------------------");
            docList = new List <Document>();
            Document infoDoc;

            do
            {
                try
                {
                    docList = search.GetNextSet();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("\n Error: Search.GetNextStep failed because: " + ex.Message);
                    break;
                }
                foreach (var doc in docList)
                {
                    infoDoc = doc["info"].AsDocument();
                    Console.WriteLine(movieFormatString,
                                      doc["title"],
                                      infoDoc["actors"].AsArrayOfString()[0],
                                      string.Join(commaSep, infoDoc["genres"].AsArrayOfString()));
                }
            } while (!search.IsDone);


            /*-----------------------------------------------------------------------
             *  4.1.2b:  Call AmazonDynamoDBClient.Query to initiate a query for all
             *           movies where year equals 1992 AND title is between M and Tzz,
             *           returning the genres and the lead actor of each.
             *-----------------------------------------------------------------------*/
            QueryRequest qRequest = new QueryRequest
            {
                TableName = "Movies",
                ExpressionAttributeNames = new Dictionary <string, string>
                {
                    { "#yr", "year" }
                },
                ExpressionAttributeValues = new Dictionary <string, AttributeValue>
                {
                    { ":y_1992", new AttributeValue {
                          N = "1992"
                      } },
                    { ":M", new AttributeValue {
                          S = "M"
                      } },
                    { ":Tzz", new AttributeValue {
                          S = "Tzz"
                      } }
                },
                KeyConditionExpression = "#yr = :y_1992 and title between :M and :Tzz",
                ProjectionExpression   = "title, info.actors[0], info.genres"
            };

            QueryResponse qResponse;

            try
            {
                qResponse = client.Query(qRequest);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n Error: Low-level query failed, because: " + ex.Message);
                PauseForDebugWindow();
                return;
            }

            // Display the movie information returned by this query
            Console.WriteLine("\n\n Movies released in 1992 with titles between \"M\" and \"Tzz\" (low-level):" +
                              "\n-------------------------------------------------------------------------");
            foreach (Dictionary <string, AttributeValue> item in qResponse.Items)
            {
                Dictionary <string, AttributeValue> info = item["info"].M;
                Console.WriteLine(movieFormatString,
                                  item["title"].S,
                                  info["actors"].L[0].S,
                                  GetDdbListAsString(info["genres"].L));
            }
        }