php - DynamoDb retrieve data Order by descending order and then use pagination like sql syntax -
i facing problem retrieve records in descending order pagination limit amazon dynamodb in mysql. using following script, gives unordered list of records. need last inserted id on top.
$limit = 10; $total = 0; $start_key = null; $params = array('tablename' => 'event','attributestoget' =>array('id','interactiondate','repname','totalamount','fooding','nonfooding','pdfdocument','ismultiple','payment_mode','interaction_type','products','programtitle','venue','workstepid','foodingother','interaction_type_other'), 'scanfilter'=> array('manufacturername' => array("comparisonoperator" => "eq", "attributevaluelist" => array(array("s" => "$manufacturername")))),'limit'=>$limit ); $itemsarray = array(); $itemsarray = array(); $finalitemsarray = array(); $finalcrmrecords = array(); do{ if(!empty($start_key)){ $params['exclusivestartkey'] = $start_key->getarraycopy(); } $response = $this->amazon->dynamodb->scan($params); if ($response->status == 200) { $counter = (string) $response->body->count; $total += $counter; foreach($response->body->items $itemsarray){ $finalitemsarray[] = $itemsarray; } if($total>$limit){ $i =1; foreach($response->body->items $items){ $finalitemsarray[] = $items; if($i == $limit){ $start_key = $items->id->{amazondynamodb::type_number}->to_array(); $finalcrmrecords['data'] = $finalitemsarray; $finalcrmrecords['start_key'] = $start_key; break; } $i++; } }elseif($total<$limit){ $start_key = $response->body->lastevaluatedkey->to_array(); }else{ $finalcrmrecords['data'] = $finalitemsarray; if ($response->body->lastevaluatedkey) { $start_key =$response->body->lastevaluatedkey->to_array(); break; } else { $start_key = null; } $finalcrmrecords['start_key'] = $start_key; } } }while($start_key);
regards
sandeep kumar sinha
a scan
operation in dynamodb can not change sorting of returned items. scan
pretty expensive operation requires read whole table.
if want take advantage of dynamodb, here's 1 advice:
instead of looking information, try find it.
in sense of, use lookups instead of scan/query information need.
as example, if have table stores events. store events in table, eventid hashkey
. can have second table eventlookups store lookups eventids. in eventlookups table put item
lookupid: latest-event
referencing eventid: ...
. every time insert new events can update latest-event entry point newer event. or use set store latest 50 eventids events in 1 item.
-mathias
Comments
Post a Comment