AWS DynamoDB批量获取请求 – iOS

我可以在AWS dynamoDB中的单个表上执行一个简单的Get请求,但是当我将其扩展到跨多个表的批量请求时,我仍然会收到一个错误

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

我理解这是值不被正确传递,但我不明白是什么问题与我的代码

 //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; }]; 

search互联网的高和低,但不能看到一个批处理请求使用iOS Objective C(或迅速的事情)的工作示例。

我已经testing了两个variables在一个单一的Get请求,他们都工作。

您忘记了AWSDynamoDBAttributeValue中的AWSDynamoDBKeysAndAttributes 。 下面是AWSDynamoDBTests.m的一个简单例子:

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

由于批处理get不映射到一个类,我通过这样做来解决它。

我这样做解决了它,

  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 } 

Swift 3

我能够得到BatchGet请求使用下面的代码。 希望这可以帮助那些正在为缺乏Swift Docs而苦苦挣扎的人。

  • 此代码假定您已经在AppDelegate应用程序didFinishLaunchingWithOptions方法中configuration了您的AWSServiceConfiguration。

     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!) } }