I am having a list of items, can i implement pagination on location attribute e.g fetch from indexes 0 to 10, 10 to 20...
{
continent:String,
country:String,
locations:[ {address,phone},{address,phone}....]
}
The DynamoDB read requests (e.g., GetItem
) have a ProjectionExpression
option where you can pass a list of attributes as well as sub-attributes you want to fetch from the item instead of the whole item. So you can use
ProjectionExpression="locations[10],locations[11],...,locations[19]"
to have GetItem
return just elements 10..19 from the "locations" list. Unfortunately there is no shorter syntax to get a range of indices.
However, you should be aware that even if you ask to only read elements 10..19 from the list, you'll actually be paying to read the entire item. DynamoDB assumes that items (the whole thing in your example) are fairly small (the hard limit is 400 KB), and any read or write to an item - even if just to a part of it - incurs a cost proportional to the length of the entire item. So although you could do paging using the syntax I just described, it not usually not a cost-effective approach. If you read an item in 20 "pages", you'll be paying for reading the same item 20 times! You're better off just fetching the entire item (again, it's limited in size anyway) once and paging through it in the client.
When talking about paging being an effective approach, the discussion is usual about multiple items in the same partition, i.e., multiple items with the same hash key and different sort key. You can then page through these items using the Query
request, and you will be paying just for the items you paged through. With such approach the length of the "list" (it's not really a list, but rather many separate items) is practically unbounded.