AWS DynamoDB 批量获取请求 - iOS
AWS DynamoDB Batch Get Request - iOS
我可以在 AWS dynamoDB 中对单个 table 执行简单的 Get
请求,但是当我将其扩展为跨多个 table 的批处理请求时,我继续收到错误消息
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;
}];
在互联网上到处搜索,但看不到使用 iOS Objective C(或 swift 的批处理请求的工作示例)。
我已经在单个 Get
请求中测试了两个变量,它们都有效。
您忘记在 AWSDynamoDBKeysAndAttributes
中环绕 AWSDynamoDBAttributeValue
。这是来自 AWSDynamoDBTests.m 的一个简单示例:
AWSDynamoDBKeysAndAttributes *keysAndAttributes = [AWSDynamoDBKeysAndAttributes new];
keysAndAttributes.keys = @[@{@"hashKey" : attributeValue1},
@{@"hashKey" : attributeValue2}];
keysAndAttributes.consistentRead = @YES;
AWSDynamoDBBatchGetItemInput *batchGetItemInput = [AWSDynamoDBBatchGetItemInput new];
batchGetItemInput.requestItems = @{table1Name: keysAndAttributes};
由于批量获取没有映射到 class 我通过这样做解决了它。
我这样做解决了,
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 文档而苦苦挣扎的人。
此代码假定您已在 AppDelegate 应用程序 didFinishLaunchingWithOptions 方法中配置了 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!)
}
}
我可以在 AWS dynamoDB 中对单个 table 执行简单的 Get
请求,但是当我将其扩展为跨多个 table 的批处理请求时,我继续收到错误消息
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;
}];
在互联网上到处搜索,但看不到使用 iOS Objective C(或 swift 的批处理请求的工作示例)。
我已经在单个 Get
请求中测试了两个变量,它们都有效。
您忘记在 AWSDynamoDBKeysAndAttributes
中环绕 AWSDynamoDBAttributeValue
。这是来自 AWSDynamoDBTests.m 的一个简单示例:
AWSDynamoDBKeysAndAttributes *keysAndAttributes = [AWSDynamoDBKeysAndAttributes new];
keysAndAttributes.keys = @[@{@"hashKey" : attributeValue1},
@{@"hashKey" : attributeValue2}];
keysAndAttributes.consistentRead = @YES;
AWSDynamoDBBatchGetItemInput *batchGetItemInput = [AWSDynamoDBBatchGetItemInput new];
batchGetItemInput.requestItems = @{table1Name: keysAndAttributes};
由于批量获取没有映射到 class 我通过这样做解决了它。
我这样做解决了,
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 文档而苦苦挣扎的人。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!)
}
}