Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP AWS DynamoDB: Limit the Number of Total Query Results Using an Iterator

I am trying to limit the total number of results that can come back with an iterator to 3. Not the number of results for each iteration. I am hoping to make this number dynamic. However, I cannot find any real answer to accomplish this and the documentation that AWS provides is of no help. Putting the Limit inside array with the table name and keys doesn't limit the results. I also put it in its own separate array but that doesn't work either. The below is what I have tried but I have not been able to get this to work. Any help would be greatly appreciated.

$iterator = $dbh->getIterator('Query', array(
        'TableName'     => 'raw',
        'KeyConditions' => array(
            'deviceID' => array(
                'AttributeValueList' => array(
                    array('S' => $deviceID)
                ),
                'ComparisonOperator' => 'EQ'
            )
        ),
        'ScanIndexForward' => false   // true = ascending, false = descending
    ),
    array(
        'Limit' => 3 
    )
);

Then putting the resulting data into an array. I don't know if I need to do something different here?

foreach ($iterator as $item) {
     array_push($resultArray, $item);
}
like image 829
applecrusher Avatar asked Nov 07 '22 11:11

applecrusher


1 Answers

You might have already tried this, by the looks of it, but try your code with lowercase 'limit'.

 $iterator = $dbh->getIterator('Query', array(
        'TableName'     => 'raw',
        'KeyConditions' => array(
            'deviceID' => array(
                'AttributeValueList' => array(
                    array('S' => $deviceID)
                ),
                'ComparisonOperator' => 'EQ'
            )
        ),
        'ScanIndexForward' => false   // true = ascending, false = descending
    ),
    array(
        'limit' => 3 
    )
);

You could also try limiting by doing below.

foreach (new LimitIterator($iterator, 0, 3) as $item) {
    array_push($resultArray, $item); 
}
like image 199
Kamal Soni Avatar answered Nov 14 '22 21:11

Kamal Soni