from api docs dynamo db support pagination scan , query operations. catch here set exclusivestartindex
of current request value of lastevaluatedindex
of previous request next set (logical page) of results.
i'm trying implement same i'm using dynamodbmapper
, seems have lot more advantages tight coupling data models. if wanted above, i'm assuming below:
// mapping of hashkey of last item in previous query operation map<string, attributevalue> lasthashkey = .. dynamodbqueryexpression expression = new dynamodbqueryexpression(); ... expression.setexclusivestartkey(); list<table> nextpageresults = mapper.query(table.class, expression);
i hope above understanding correct on paginating using dynamodbmapper. secondly, how know i've reached end of results. docs if use following api:
queryresult result = dynamodbclient.query((queryrequest) request); boolean isendofresults = stringutils.isempty(result.getlastevaluatedkey());
coming using dynamodbmapper, how can know if i've reached end of results in case.
you have couple different options dynamodbmapper
, depending on way want go.
query
- returnspaginatedquerylist
querypage
- returnsqueryresultpage
scan
- returnspaginatedscanlist
scanpage
- returnsscanresultpage
the part here understanding difference between methods, , functionality returned objects encapsulate.
i'll go on paginatedscanlist
, scanresultpage
, these methods/objects mirror each other.
the paginatedscanlist
says following, emphasis mine:
implementation of list interface represents results scan in aws dynamodb. paginated results loaded on demand when user executes operation requires them. operations, such size(), must fetch entire list, results lazily fetched page page when possible.
this says results loaded iterate through list. when through first page, second page automatically fetched out having explicitly make request. lazy loading results default method, can overridden if call overloaded methods , supply dynamodbmapperconfig
different dynamodbmapperconfig.paginationloadingstrategy
.
this different scanresultpage
. given page of results, , deal pagination yourself.
here quick code sample showing example usage of both methods ran table of 5 items using dynamodblocal:
final dynamodbmapper mapper = new dynamodbmapper(client); // using 'paginatedscanlist' final dynamodbscanexpression paginatedscanlistexpression = new dynamodbscanexpression() .withlimit(limit); final paginatedscanlist<myclass> paginatedlist = mapper.scan(myclass.class, paginatedscanlistexpression); paginatedlist.foreach(system.out::println); system.out.println(); // using 'scanresultpage' final dynamodbscanexpression scanpageexpression = new dynamodbscanexpression() .withlimit(limit); { scanresultpage<myclass> scanpage = mapper.scanpage(myclass.class, scanpageexpression); scanpage.getresults().foreach(system.out::println); system.out.println("lastevaluatedkey=" + scanpage.getlastevaluatedkey()); scanpageexpression.setexclusivestartkey(scanpage.getlastevaluatedkey()); } while (scanpageexpression.getexclusivestartkey() != null);
and output:
myclass{hash=2} myclass{hash=1} myclass{hash=3} myclass{hash=0} myclass{hash=4} myclass{hash=2} myclass{hash=1} lastevaluatedkey={hash={n: 1,}} myclass{hash=3} myclass{hash=0} lastevaluatedkey={hash={n: 0,}} myclass{hash=4} lastevaluatedkey=null
Comments
Post a Comment