我正在尝试使用 AWS Dynamo DB 构建应用程序,我想查询或扫描我的数据库,但是我只能使用一个参数扫描数据库,但我无法使用数组查询数据库。
例如,我的数据库中有一个表user details
,主键UserID
。我想得到几行,它们的UserID
存储在数组中。
这是我尝试使用的代码,但它对我不起作用。有人可以帮我吗?谢谢。
NSArray *Array = [[NSArray alloc]initWithObjects:@"001", nil];
NSMutableDictionary *Diction = [NSMutableDictionary dictionary];
[Diction setObject:[NSString stringWithFormat:@"%@",Array]
forKey:@":val"];
AWSDynamoDBObjectMapper *dynamoDBObjectMapper =
[AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
UserDetails_Male *User = [UserDetails_Male new];
AWSDynamoDBScanExpression *scanExpression = [AWSDynamoDBScanExpression
new];
scanExpression.filterExpression = @"UserID = :val";
scanExpression.expressionAttributeValues = @{@":val":Array};
[[dynamoDBObjectMapper scan:[User class]
expression:scanExpression]
continueWithBlock:^id(AWSTask *task) {
if (task.error) {
NSLog(@"The request failed. Error: [%@]", task.error);
} else {
AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
for (UserDetails_Male *book in paginatedOutput.items) {
//Do something with book.
NSLog(@"Data: %@",book);
}
}
return nil;
}];
-(void)BatchReq{
AWSDynamoDBKeysAndAttributes * keysAndAttributes = [
AWSDynamoDBKeysAndAttributes new ];
AWSDynamoDBAttributeValue * attributeValue2 = [
AWSDynamoDBAttributeValue new ];
attributeValue2.SS = Array;
keysAndAttributes.keys = @[ @{ @"UserId" :
attributeValue1 }, ];
keysAndAttributes.consistentRead = @YES;
AWSDynamoDBBatchGetItemInput * batchGetItemInput = [
AWSDynamoDBBatchGetItemInput new ];
batchGetItemInput.requestItems = @{ @"DynamoDB-OM-Sample"
: keysAndAttributes };
AWSDynamoDB * awsDynamoDB = [ AWSDynamoDB defaultDynamoDB
];
[ [ awsDynamoDB batchGetItem: batchGetItemInput ]
continueWithExecutor: [ AWSExecutor mainThreadExecutor ]
withBlock: ^ id ( AWSTask * task ) {
if ( task.result ) {
NSLog ( @"it's working!!" );
}
else {
NSLog ( @"not working... " );
}
return nil;
} ];
如果已将 HashKey 设置为用户 ID,请使用 BatchGetItem API 批量获取数组中的所有 ID。
在此处查看有关 BatchGetItem 的更多信息:http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html