public override void Invoke(AWSCredentials creds, RegionEndpoint region, int maxItems)
        {
            AmazonDynamoDBConfig config = new AmazonDynamoDBConfig();

            config.RegionEndpoint = region;
            ConfigureClient(config);
            AmazonDynamoDBClient client = new AmazonDynamoDBClient(creds, config);

            BatchGetItemResponse resp = new BatchGetItemResponse();

            do
            {
                BatchGetItemRequest req = new BatchGetItemRequest
                {
                    RequestItems = resp.UnprocessedKeys
                };

                resp = client.BatchGetItem(req);
                CheckError(resp.HttpStatusCode, "200");

                foreach (var obj in resp.Responses)
                {
                    AddObject(obj);
                }

                foreach (var obj in resp.UnprocessedKeys)
                {
                    AddObject(obj);
                }
            }while (resp.UnprocessedKeys.Count > 0);
        }
Example #2
0
        public void DynamoDBBatchGetItem()
        {
            #region batch-get-item-1435614252853

            var client   = new AmazonDynamoDBClient();
            var response = client.BatchGetItem(new BatchGetItemRequest
            {
                RequestItems = new Dictionary <string, KeysAndAttributes> {
                    { "tablename1", new KeysAndAttributes {
                          Keys = new List <Dictionary <string, AttributeValue> > {
                              new Dictionary <string, AttributeValue> {
                                  { "key1", new AttributeValue {
                                        N = "NumberAttributeValue"
                                    } }
                              }
                          },
                          AttributesToGet = new List <string> {
                              "attr1",
                              "attr2"
                          },
                          ConsistentRead           = true,
                          ProjectionExpression     = "str1",
                          ExpressionAttributeNames = new Dictionary <string, string> {
                              { "name1", "attr3" },
                              { "name2", "attr4" }
                          }
                      } },
                    { "tablename2", new KeysAndAttributes {
                          Keys = new List <Dictionary <string, AttributeValue> > {
                              new Dictionary <string, AttributeValue> {
                                  { "key2", new AttributeValue {
                                        BOOL = true
                                    } }
                              }
                          },
                          AttributesToGet = new List <string> {
                              "attr1",
                              "attr2"
                          },
                          ConsistentRead           = false,
                          ProjectionExpression     = "str2",
                          ExpressionAttributeNames = new Dictionary <string, string> {
                              { "name1", "attr3" },
                              { "name2", "attr4" }
                          }
                      } }
                },
                ReturnConsumedCapacity = "NONE"
            });

            Dictionary <string, List <Dictionary <string, AttributeValue> > > responses = response.Responses;
            List <ConsumedCapacity> consumedCapacity = response.ConsumedCapacity;

            #endregion
        }
Example #3
0
        public void TestRequestResponseParameterAndDescriptorForAWSSDKHandler()
        {
            using (var client = new AmazonDynamoDBClient(new AnonymousAWSCredentials(), RegionEndpoint.USEast1))
            {
                CustomResponses.SetResponse(client, null, null, true);
                _recorder.BeginSegment("test", TraceId);
                var segment = TraceContext.GetEntity();

                var key1 = new Dictionary <string, AttributeValue>()
                {
                    { "id", new AttributeValue("1") }
                };
                var key2 = new Dictionary <string, AttributeValue>()
                {
                    { "id", new AttributeValue("2") }
                };
                var keys = new KeysAndAttributes()
                {
                    Keys = new List <Dictionary <string, AttributeValue> >()
                    {
                        key1, key2
                    }
                };

#if NET45
                client.BatchGetItem(new Dictionary <string, KeysAndAttributes>()
                {
                    { "test", keys }
                });
#else
                client.BatchGetItemAsync(new Dictionary <string, KeysAndAttributes>()
                {
                    { "test", keys }
                }).Wait();
#endif
                _recorder.EndSegment();
                Assert.IsTrue(segment.Subsegments[0].Aws.ContainsKey("request_items"));
                Assert.IsTrue(segment.Subsegments[0].Aws.ContainsKey("responses"));
                Assert.IsTrue(segment.Subsegments[0].Aws.ContainsKey("item_count"));
                Assert.IsTrue(segment.Subsegments[0].Aws.ContainsKey("table_names"));
                Assert.IsTrue(segment.Subsegments[0].Aws.ContainsKey("operation"));
                Assert.IsTrue(segment.Subsegments[0].Aws.ContainsKey("request_id"));
            }
        }
        private static void RetrieveMultipleItemsBatchGet()
        {
            var request = new BatchGetItemRequest
            {
                RequestItems = new Dictionary <string, KeysAndAttributes>()
                {
                    { table1Name,
                      new KeysAndAttributes
                      {
                          Keys = new List <Dictionary <string, AttributeValue> >()
                          {
                              new Dictionary <string, AttributeValue>()
                              {
                                  { "Name", new AttributeValue {
                                    S = "Amazon DynamoDB"
                                } }
                              },
                              new Dictionary <string, AttributeValue>()
                              {
                                  { "Name", new AttributeValue {
                                    S = "Amazon S3"
                                } }
                              }
                          }
                      } },
                    {
                        table2Name,
                        new KeysAndAttributes
                        {
                            Keys = new List <Dictionary <string, AttributeValue> >()
                            {
                                new Dictionary <string, AttributeValue>()
                                {
                                    { "ForumName", new AttributeValue {
                                          S = "Amazon DynamoDB"
                                      } },
                                    { "Subject", new AttributeValue {
                                          S = "DynamoDB Thread 1"
                                      } }
                                },
                                new Dictionary <string, AttributeValue>()
                                {
                                    { "ForumName", new AttributeValue {
                                          S = "Amazon DynamoDB"
                                      } },
                                    { "Subject", new AttributeValue {
                                          S = "DynamoDB Thread 2"
                                      } }
                                },
                                new Dictionary <string, AttributeValue>()
                                {
                                    { "ForumName", new AttributeValue {
                                          S = "Amazon S3"
                                      } },
                                    { "Subject", new AttributeValue {
                                          S = "S3 Thread 1"
                                      } }
                                }
                            }
                        }
                    }
                }
            };

            BatchGetItemResponse response;

            do
            {
                Console.WriteLine("Making request");
                response = client.BatchGetItem(request);

                // Check the response.
                var responses = response.Responses; // Attribute list in the response.

                foreach (var tableResponse in responses)
                {
                    var tableResults = tableResponse.Value;
                    Console.WriteLine("Items retrieved from table {0}", tableResponse.Key);
                    foreach (var item1 in tableResults)
                    {
                        PrintItem(item1);
                    }
                }

                // Any unprocessed keys? could happen if you exceed ProvisionedThroughput or some other error.
                Dictionary <string, KeysAndAttributes> unprocessedKeys = response.UnprocessedKeys;
                foreach (var unprocessedTableKeys in unprocessedKeys)
                {
                    // Print table name.
                    Console.WriteLine(unprocessedTableKeys.Key);
                    // Print unprocessed primary keys.
                    foreach (var key in unprocessedTableKeys.Value.Keys)
                    {
                        PrintItem(key);
                    }
                }

                request.RequestItems = unprocessedKeys;
            } while (response.UnprocessedKeys.Count > 0);
        }
Example #5
0
        public void BatchSamples()
        {
            EnsureTables();

            {
                #region BatchGet Sample 1

                // Define attributes to get and keys to retrieve
                List <string> attributesToGet = new List <string> {
                    "Author", "Title", "Year"
                };
                List <Dictionary <string, AttributeValue> > sampleTableKeys = new List <Dictionary <string, AttributeValue> >
                {
                    new Dictionary <string, AttributeValue>
                    {
                        { "Author", new AttributeValue {
                              S = "Mark Twain"
                          } },
                        { "Title", new AttributeValue {
                              S = "The Adventures of Tom Sawyer"
                          } }
                    },
                    new Dictionary <string, AttributeValue>
                    {
                        { "Author", new AttributeValue {
                              S = "Mark Twain"
                          } },
                        { "Title", new AttributeValue {
                              S = "Adventures of Huckleberry Finn"
                          } }
                    }
                };

                // Construct get-request for first table
                KeysAndAttributes sampleTableItems = new KeysAndAttributes
                {
                    AttributesToGet = attributesToGet,
                    Keys            = sampleTableKeys
                };

                #endregion

                #region BatchGet Sample 2

                // Define keys to retrieve
                List <Dictionary <string, AttributeValue> > authorsTableKeys = new List <Dictionary <string, AttributeValue> >
                {
                    new Dictionary <string, AttributeValue>
                    {
                        { "Author", new AttributeValue {
                              S = "Mark Twain"
                          } },
                    },
                    new Dictionary <string, AttributeValue>
                    {
                        { "Author", new AttributeValue {
                              S = "Booker Taliaferro Washington"
                          } },
                    }
                };

                // Construct get-request for second table
                //  Skip setting AttributesToGet property to retrieve all attributes
                KeysAndAttributes authorsTableItems = new KeysAndAttributes
                {
                    Keys = authorsTableKeys,
                };

                #endregion

                #region BatchGet Sample 3

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Construct table-keys mapping
                Dictionary <string, KeysAndAttributes> requestItems = new Dictionary <string, KeysAndAttributes>();
                requestItems["SampleTable"]  = sampleTableItems;
                requestItems["AuthorsTable"] = authorsTableItems;

                // Construct request
                BatchGetItemRequest request = new BatchGetItemRequest
                {
                    RequestItems = requestItems
                };

                BatchGetItemResult result;
                do
                {
                    // Issue request and retrieve items
                    result = client.BatchGetItem(request);

                    // Iterate through responses
                    Dictionary <string, List <Dictionary <string, AttributeValue> > > responses = result.Responses;
                    foreach (string tableName in responses.Keys)
                    {
                        // Get items for each table
                        List <Dictionary <string, AttributeValue> > tableItems = responses[tableName];

                        // View items
                        foreach (Dictionary <string, AttributeValue> item in tableItems)
                        {
                            Console.WriteLine("Item:");
                            foreach (var keyValuePair in item)
                            {
                                Console.WriteLine("{0} : S={1}, N={2}, SS=[{3}], NS=[{4}]",
                                                  keyValuePair.Key,
                                                  keyValuePair.Value.S,
                                                  keyValuePair.Value.N,
                                                  string.Join(", ", keyValuePair.Value.SS ?? new List <string>()),
                                                  string.Join(", ", keyValuePair.Value.NS ?? new List <string>()));
                            }
                        }
                    }

                    // Some items may not have been retrieved!
                    //  Set RequestItems to the result's UnprocessedKeys and reissue request
                    request.RequestItems = result.UnprocessedKeys;
                } while (result.UnprocessedKeys.Count > 0);

                #endregion
            }


            {
                #region BatchWrite Sample 1

                // Create items to put into first table
                Dictionary <string, AttributeValue> item1 = new Dictionary <string, AttributeValue>();
                item1["Author"] = new AttributeValue {
                    S = "Mark Twain"
                };
                item1["Title"] = new AttributeValue {
                    S = "A Connecticut Yankee in King Arthur's Court"
                };
                item1["Pages"] = new AttributeValue {
                    N = "575"
                };
                Dictionary <string, AttributeValue> item2 = new Dictionary <string, AttributeValue>();
                item2["Author"] = new AttributeValue {
                    S = "Booker Taliaferro Washington"
                };
                item2["Title"] = new AttributeValue {
                    S = "My Larger Education"
                };
                item2["Pages"] = new AttributeValue {
                    N = "313"
                };
                item2["Year"] = new AttributeValue {
                    N = "1911"
                };

                // Create key for item to delete from first table
                //  Hash-key of the target item is string value "Mark Twain"
                //  Range-key of the target item is string value "Tom Sawyer, Detective"
                Dictionary <string, AttributeValue> keyToDelete1 = new Dictionary <string, AttributeValue>
                {
                    { "Author", new AttributeValue {
                          S = "Mark Twain"
                      } },
                    { "Title", new AttributeValue {
                          S = "Tom Sawyer, Detective"
                      } }
                };

                // Construct write-request for first table
                List <WriteRequest> sampleTableItems = new List <WriteRequest>();
                sampleTableItems.Add(new WriteRequest
                {
                    PutRequest = new PutRequest {
                        Item = item1
                    }
                });
                sampleTableItems.Add(new WriteRequest
                {
                    PutRequest = new PutRequest {
                        Item = item2
                    }
                });
                sampleTableItems.Add(new WriteRequest
                {
                    DeleteRequest = new DeleteRequest {
                        Key = keyToDelete1
                    }
                });

                #endregion

                #region BatchWrite Sample 2

                // Create key for item to delete from second table
                //  Hash-key of the target item is string value "Francis Scott Key Fitzgerald"
                Dictionary <string, AttributeValue> keyToDelete2 = new Dictionary <string, AttributeValue>
                {
                    { "Author", new AttributeValue {
                          S = "Francis Scott Key Fitzgerald"
                      } },
                };

                // Construct write-request for first table
                List <WriteRequest> authorsTableItems = new List <WriteRequest>();
                authorsTableItems.Add(new WriteRequest
                {
                    DeleteRequest = new DeleteRequest {
                        Key = keyToDelete2
                    }
                });

                #endregion

                #region BatchWrite Sample 3

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Construct table-keys mapping
                Dictionary <string, List <WriteRequest> > requestItems = new Dictionary <string, List <WriteRequest> >();
                requestItems["SampleTable"]  = sampleTableItems;
                requestItems["AuthorsTable"] = authorsTableItems;

                BatchWriteItemRequest request = new BatchWriteItemRequest {
                    RequestItems = requestItems
                };
                BatchWriteItemResult result;
                do
                {
                    // Issue request and retrieve items
                    result = client.BatchWriteItem(request);

                    // Some items may not have been processed!
                    //  Set RequestItems to the result's UnprocessedItems and reissue request
                    request.RequestItems = result.UnprocessedItems;
                } while (result.UnprocessedItems.Count > 0);

                #endregion
            }
        }