4

I can perform a simple Get request on a singular table within AWS dynamoDB however when I expand it to a Batch Request across multiple tables I continue to get a error

validation error detected: Value null at 'requestItems.rip.member.keys' failed to satisfy constraint

I understand this as the values not being passed correctly but I can't see what the issue is with my code

//Create Request Values
AWSDynamoDBGetItemInput *getItem = [AWSDynamoDBGetItemInput new];
AWSDynamoDBAttributeValue *hashValue = [AWSDynamoDBAttributeValue new];
hashValue.S = @"User Test";
getItem.key = @{@"ripId": hashValue};

//Create Request Values 2 
AWSDynamoDBGetItemInput *getItem2 = [AWSDynamoDBGetItemInput new];
AWSDynamoDBAttributeValue *hashValue2 = [AWSDynamoDBAttributeValue new];
hashValue2.S = @"User Test";
getItem2.key = @{@"chat": hashValue2};

//Combine to Batch Request
AWSDynamoDBBatchGetItemInput * batchFetch = [AWSDynamoDBBatchGetItemInput new];
batchFetch.requestItems = @{ @"rip": getItem,
                             @"chat": getItem,};

[[dynamoDB batchGetItem:batchFetch] continueWithBlock:^id(BFTask *task) {
    if (!task.error) {

        NSLog(@"BOY SUCCES");

    } else {
        NSLog(@" NO BOY SUCCESS %@",task.error);
    }
    return nil;
}];

Searched the internet high and low but cannot see a working example of a batch request using iOS Objective C (or swift for that matter).

I have tested both variables on a single Get request and they both work.

memyselfandmyiphone
  • 1,080
  • 4
  • 21
  • 43
  • From what I've read this batch get item can't be used with the AWSDynamoDBObjectMapper? Is there a way mourned this, or does one manually have to iterate through and create the objects form the raw data? – Niklas Mar 09 '16 at 14:06

3 Answers3

3

You forgot to wrap around AWSDynamoDBAttributeValue in AWSDynamoDBKeysAndAttributes. Here is a simple example from AWSDynamoDBTests.m:

AWSDynamoDBKeysAndAttributes *keysAndAttributes = [AWSDynamoDBKeysAndAttributes new];
keysAndAttributes.keys = @[@{@"hashKey" : attributeValue1},
                           @{@"hashKey" : attributeValue2}];
keysAndAttributes.consistentRead = @YES;

AWSDynamoDBBatchGetItemInput *batchGetItemInput = [AWSDynamoDBBatchGetItemInput new];
batchGetItemInput.requestItems = @{table1Name: keysAndAttributes};
Yosuke
  • 3,759
  • 1
  • 11
  • 21
1

Since the batch get doesn't map to a class I solved it by doing this instead.

I solved it by doing this,

    let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.defaultDynamoDBObjectMapper()
    let task1 = dynamoDBObjectMapper.load(User.self, hashKey: "rtP1oQ5DJG", rangeKey: nil)
    let task2 = dynamoDBObjectMapper.load(User.self, hashKey: "dbqb1zyUq1", rangeKey: nil)

    AWSTask.init(forCompletionOfAllTasksWithResults: [task1, task2]).continueWithBlock { (task) -> AnyObject? in
        if let users = task.result as? [User] {
            print(users.count)
            print(users[0].firstName)
            print(users[1].firstName)
        }
        else if let error = task.error {
            print(error.localizedDescription)
        }
        return nil
    }
Niklas
  • 1,322
  • 14
  • 11
0

Swift 3

I was able to get the BatchGet request work with the following code. Hope this helps someone else who's struggling with the lack of Swift Docs.
  • This code assumes that you've configured your AWSServiceConfiguration in the AppDelegate application didFinishLaunchingWithOptions method.
    let DynamoDB = AWSDynamoDB.default()
    
        // define your primary hash keys
        let hashAttribute1 = AWSDynamoDBAttributeValue()
        hashAttribute1?.s = "NDlFRTdDODEtQzNCOC00QUI5LUFFMzUtRkIyNTJFNERFOTBF"
    
        let hashAttribute2 = AWSDynamoDBAttributeValue()
        hashAttribute2?.s = "MjVCNzU3MUQtMEM0NC00NEJELTk5M0YtRTM0QjVDQ0Q1NjlF"
    
        let keys: Array = [["userID": hashAttribute1], ["userID": hashAttribute2]]
    
        let keysAndAttributesMap = AWSDynamoDBKeysAndAttributes()
        keysAndAttributesMap?.keys = keys as? [[String : AWSDynamoDBAttributeValue]]
        keysAndAttributesMap?.consistentRead = true
        let tableMap = ["Your-Table-Name" : keysAndAttributesMap]
        let request = AWSDynamoDBBatchGetItemInput()
        request?.requestItems = tableMap as? [String : AWSDynamoDBKeysAndAttributes]
        request?.returnConsumedCapacity = AWSDynamoDBReturnConsumedCapacity.total
        DynamoDB.batchGetItem(request!) { (output, error) in
            if output != nil {
                print("Batch Query output?.responses?.count:", output!.responses!)
            }
            if error != nil {
                print("Batch Query error:", error!)
            }
        }
    
  • inga
    • 1