【问题标题】:Query AWS Dynamo Db Data base using an Array使用数组查询 AWS Dynamodb 数据库
【发布时间】:2017-10-13 13:49:45
【问题描述】:

我正在尝试使用 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;
               } ];

【问题讨论】:

    标签: ios amazon-web-services aws-sdk-ios aws-ios


    【解决方案1】:

    如果您已将HashKey设置为用户ID,请使用BatchGetItem API批量获取数组中的所有ID。

    在此处查看有关 BatchGetItem 的更多信息: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html

    【讨论】:

    • 嘿,非常感谢您的回复,我编辑了我的问题并添加了函数 BatchReq,该代码可以工作吗?还是必须使用 for 循环来创建多个属性并将它们作为键传递。?
    • 我对 Objective-C 或 Swift 知之甚少。所以我可能无法为您提供代码帮助。我可以为您指出正确的方法。
    猜你喜欢
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    相关资源
    最近更新 更多