private static BatchGetItemRequest CreateRequest(Dictionary <string, RequestSet> set, Table targetTable, bool isAsync) { BatchGetItemRequest request = new BatchGetItemRequest(); ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)request).AddBeforeRequestHandler(isAsync ? new RequestEventHandler(targetTable.UserAgentRequestEventHandlerAsync) : new RequestEventHandler(targetTable.UserAgentRequestEventHandlerSync) ); var requestItems = new Dictionary <string, KeysAndAttributes>(); foreach (var kvp in set) { var tableName = kvp.Key; var requestSet = kvp.Value; var keys = new KeysAndAttributes { Keys = requestSet.GetItems(), ConsistentRead = requestSet.Batch.ConsistentRead, AttributesToGet = requestSet.Batch.AttributesToGet }; requestItems.Add(tableName, keys); } request.RequestItems = requestItems; return(request); }
public List <T> GetItems <T>(IEnumerable <DynamoId> ids) { var to = new List <T>(); var table = DynamoMetadata.GetTable <T>(); var remainingIds = ids.ToList(); while (remainingIds.Count > 0) { var batchSize = Math.Min(remainingIds.Count, MaxReadBatchSize); var nextBatch = remainingIds.GetRange(0, batchSize); remainingIds.RemoveRange(0, batchSize); var getItems = new KeysAndAttributes { ConsistentRead = ConsistentRead, }; nextBatch.Each(id => getItems.Keys.Add(Converters.ToAttributeKeyValue(this, table, id))); to.AddRange(ConvertBatchGetItemResponse <T>(table, getItems)); } return(to); }
private List <T> ConvertBatchGetItemResponse <T>(DynamoMetadataType table, KeysAndAttributes getItems) { var to = new List <T>(); var request = new BatchGetItemRequest(new Dictionary <string, KeysAndAttributes> { { table.Name, getItems } }); var response = Exec(() => DynamoDb.BatchGetItem(request)); List <Dictionary <string, AttributeValue> > results; if (response.Responses.TryGetValue(table.Name, out results)) { results.Each(x => to.Add(Converters.FromAttributeValues <T>(table, x))); } var i = 0; while (response.UnprocessedKeys.Count > 0) { response = Exec(() => DynamoDb.BatchGetItem(new BatchGetItemRequest(response.UnprocessedKeys))); if (response.Responses.TryGetValue(table.Name, out results)) { results.Each(x => to.Add(Converters.FromAttributeValues <T>(table, x))); } if (response.UnprocessedKeys.Count > 0) { i.SleepBackOffMultiplier(); } } return(to); }
public async Task <List <T> > GetItemsAsync <T>(IEnumerable <DynamoId> ids, CancellationToken token = default) { var to = new List <T>(); var table = DynamoMetadata.GetTable <T>(); var remainingIds = ids.ToList(); while (remainingIds.Count > 0) { var batchSize = Math.Min(remainingIds.Count, MaxReadBatchSize); var nextBatch = remainingIds.GetRange(0, batchSize); remainingIds.RemoveRange(0, batchSize); var getItems = new KeysAndAttributes { ConsistentRead = ConsistentRead, }; nextBatch.Each(id => getItems.Keys.Add(Converters.ToAttributeKeyValue(this, table, id))); to.AddRange(await ConvertBatchGetItemResponseAsync <T>(table, getItems, token).ConfigAwait()); } return(to); }
public async Task <IEnumerable <WorkflowInstance> > GetWorkflowInstances(IEnumerable <string> ids) { if (ids == null) { return(new List <WorkflowInstance>()); } var keys = new KeysAndAttributes() { Keys = new List <Dictionary <string, AttributeValue> >() }; foreach (var id in ids) { var key = new Dictionary <string, AttributeValue>() { { "id", new AttributeValue { S = id } } }; keys.Keys.Add(key); } var request = new BatchGetItemRequest { RequestItems = new Dictionary <string, KeysAndAttributes>() { { $"{_tablePrefix}-{WORKFLOW_TABLE}", keys } } }; var result = new List <Dictionary <string, AttributeValue> >(); BatchGetItemResponse response; do { response = await _client.BatchGetItemAsync(request); foreach (var tableResponse in response.Responses) { result.AddRange(tableResponse.Value); } request.RequestItems = response.UnprocessedKeys; } while (response.UnprocessedKeys.Count > 0); return(result.Select(i => i.ToWorkflowInstance())); }
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 BatchGetItemRequest CreateRequest(Dictionary <string, RequestSet> set) { BatchGetItemRequest request = new BatchGetItemRequest(); var requestItems = new Dictionary <string, KeysAndAttributes>(); foreach (var kvp in set) { var tableName = kvp.Key; var requestSet = kvp.Value; var keys = new KeysAndAttributes { Keys = requestSet.GetItems(), ConsistentRead = requestSet.Batch.ConsistentRead, AttributesToGet = requestSet.Batch.AttributesToGet }; requestItems.Add(tableName, keys); } request.RequestItems = requestItems; return(request); }
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 } }