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 return with an iterator to 3. Not the number of results for each iteration. I hope to make this number dynamic. However, I cannot find a real answer to accomplish this, and the documentation provided by AWS will not help. Placing a limit inside an array with a table name and keys does not limit the results. I also put it in my own separate array, but that doesn't work either. Below I tried, but I could not 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 put the received data into an array. I don’t know if I need to change anything here?

 foreach ($iterator as $item) { array_push($resultArray, $item); } 
+5
source share
2 answers

You may have already tried this by looking at it, but try your code with the 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 can also try to limit by doing below.

 foreach (new LimitIterator($iterator, 0, 3) as $item) { array_push($resultArray, $item); } 
+1
source

Try it. It sets Limit once inside params, which should limit the results of the query, and then once outside the parameters, which should limit the size of the iterator

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

Make sure that the argument outside the parameters is the "limit" (lowercase), and inside the "Limit" (starts with capital).

0
source

Source: https://habr.com/ru/post/1275029/


All Articles