private async Task <List <Quote> > getQuotesByIds(IEnumerable <string> ids) { // ETL List <Dictionary <string, AttributeValue> > listOfIdsToBeFetched = new List <Dictionary <string, AttributeValue> >(); foreach (string id in ids) { listOfIdsToBeFetched.Add(new Dictionary <string, AttributeValue> { { DataDefinitions.QUOTES_TABLE_HASH_KEY, new AttributeValue { N = id } } }); } BatchGetItemRequest request = new BatchGetItemRequest(new Dictionary <string, KeysAndAttributes> { { DataDefinitions.QUOTES_TABLE, new KeysAndAttributes { Keys = listOfIdsToBeFetched } } }); BatchGetItemResponse response = await _client.BatchGetItemAsync(request); // Extract data from the complex structure of response // Will return records List <Quote> quotes = new List <Quote>(); foreach (Dictionary <string, AttributeValue> pair in response.Responses[DataDefinitions.QUOTES_TABLE]) { quotes.Add(Quote.ToQuoteFromTable(pair)); } return(quotes); }
public async Task <IList <BillingAlertItem> > Get(IEnumerable <int> userIds) { var response = await _dynamoDbClient.BatchGetItemAsync(new BatchGetItemRequest(new Dictionary <string, KeysAndAttributes> { { BillingAlertStoreTableName, new KeysAndAttributes { Keys = userIds.Select(u => new Dictionary <string, AttributeValue> { { IdAttribute, new AttributeValue { N = u.ToString() } } }).ToList(), AttributesToGet = new List <string> { nameof(BillingAlertItem.CustomerId).ToLower(), nameof(BillingAlertItem.AlertAmountThreshold).ToLower(), nameof(BillingAlertItem.TotalBillAmount).ToLower(), nameof(BillingAlertItem.BillAmountLastUpdated).ToLower(), nameof(BillingAlertItem.IsAlerted).ToLower(), } } } })); var items = response.Responses.FirstOrDefault(r => r.Key == BillingAlertStoreTableName).Value; return(items.Select(i => Map(i)).ToList()); }
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")); } }
public static async void RetrieveMultipleItemsBatchGet(AmazonDynamoDBClient client) { 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 = await client.BatchGetItemAsync(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); }
public static async Task ServiceClientExampleAsync() { var client = new AmazonDynamoDBClient(RegionEndpoint.USWest2); var request = new BatchGetItemRequest { RequestItems = new Dictionary <string, KeysAndAttributes> { { //Table to read "RetailDatabase", //Definition of the keys to retrieve new KeysAndAttributes { Keys = new List <Dictionary <string, AttributeValue> > { //List of dictionaries that define the keys of the items to retrieve new Dictionary <string, AttributeValue> { { "pk", new AttributeValue { S = "*****@*****.**" } }, { "sk", new AttributeValue { S = "metadata" } } }, new Dictionary <string, AttributeValue> { { "pk", new AttributeValue { S = "*****@*****.**" } }, { "sk", new AttributeValue { S = "metadata" } } }, new Dictionary <string, AttributeValue> { { "pk", new AttributeValue { S = "*****@*****.**" } }, { "sk", new AttributeValue { S = "metadata" } } } }, ConsistentRead = true, //If you don't need consistent reads use false instead to have cheaper retrieve prices } } //You can add more tables to read here. }, ReturnConsumedCapacity = "TOTAL" }; try { //Execute the BatchGet task to obtain the data var responseBatch = await client.BatchGetItemAsync(request); //Read every table and show all the content retrieved from the table. foreach (var table in responseBatch.Responses) { Console.WriteLine($"Results of table {table.Key}: number {table.Value.Count}"); Console.WriteLine(JsonConvert.SerializeObject(table.Value)); } } catch (Exception e) { Console.Error.WriteLine(e.Message); } }
private async Task <SkillResponse> BuildWhatsNewResponseAsync() { var response = await _dynamoClient.BatchGetItemAsync(new Dictionary <string, KeysAndAttributes> { [_dynamoTable] = new KeysAndAttributes { Keys = new List <Dictionary <string, AttributeValue> > { new Dictionary <string, AttributeValue> { ["Key"] = new AttributeValue { S = PodcastInfo.ROW_KEY } }, new Dictionary <string, AttributeValue> { ["Key"] = new AttributeValue { S = MorningReportInfo.ROW_KEY } } } } }); List <Dictionary <string, AttributeValue> > rows; if ((response.HttpStatusCode != HttpStatusCode.OK) || !response.Responses.TryGetValue(_dynamoTable, out rows)) { return(BuildSpeechResponse(PROMPT_ERROR_WHAT_IS_NEW)); } MorningReportInfo morningReport = null; PodcastInfo[] podcasts = null; foreach (var row in rows) { try { switch (row["Key"].S) { case MorningReportInfo.ROW_KEY: morningReport = MorningReportInfo.FromJson(row["Value"].S); break; case PodcastInfo.ROW_KEY: podcasts = PodcastInfo.FromJson(row["Value"].S); break; default: // unexpected item; ignore it break; } } catch (Exception e) { // log the exception and continue LambdaLogger.Log($"*** ERROR: unable to parse item ({e})"); } } if ((morningReport == null) && (podcasts == null)) { return(BuildSpeechResponse(PROMPT_ERROR_WHAT_IS_NEW)); } var news = new StringBuilder(); if (morningReport != null) { news.AppendLine($"The latest morning report is from {morningReport.Date.ToString("dddd, MMMM d")}, and is entitled: \"{morningReport.Title}\"."); } if ((podcasts != null) && (podcasts.Length > 0)) { news.AppendLine($"The latest podcast was recorded {podcasts[0].Date.ToString("dddd, MMMM d")}, and is entitled: \"{podcasts[0].Title}\"."); } return(BuildSpeechResponse(news.ToString() + PROMPT_HELP_QUESTION, shouldEndSession: false)); }