iosamazon-web-servicesaws-sdk-iosaws-ios

Query AWS Dynamo Db Data base using an Array


I'm trying to build an app using AWS Dynamo Db, I wanted to query or scan my database, however I'm able to scan the database using just one parameter, but I'm unable to query the database using an array.

For example, I have a table user details in my database, with a primary key UserID. I want to get a few rows with their UserID stored in an array.

Here is the code I tried using but it's not working for me. Can someone please help me out? Thanks.

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

Solution

  • If you have set the HashKey as the user Id, use the BatchGetItem API to get all the ids in the array in bulk.

    See more about BatchGetItem here: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html