We have been using high level API’s to connect to dynamo db and fetching the data. Pagination was not setup before but since we have lot of data now, we want to setup pagination.
var scanConditions = new List<ScanCondition>();
scanConditions.Add(new ScanCondition("PartitionKey", ScanOperator.BeginsWith, "data"));
var files = await Context.ScanAsync(scanConditions).GetRemainingAsync();
return files.Select(i => i.Data).OrderByDescending(i => i.Date).ToList();
I read the aws documetation but did not find any info regarding pagination for high level api. Is pagination available for high level api? If not what options do I have here?
Thanks
2
Answers
You can use paginator methods using this .NET API
https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/DynamoDBv2/TIDynamoDBv2PaginatorFactory.html
THis is part of the AWS SDK for .NET V3.
As per your comment, here is an idea on how an implementation with low-level API would look like.
The idea is to either design the table PKs and SKs in a way that allows you to query with pagination in mind or to add GSI that allows you to query with pagination in mind.
if we assume that page size is fixed (ex: 20 items), we should be able to fetch the next page and/or fetch a specific page by page number; you would do something similar to the following.
Set PK:
ITEM#<PageIndex>
and SK:ITEM#<ItemIndex>
Keep a reference of ItemsCount (ex: PK:
ITEMSREF
, SK:ITEMSREF
,Count:
<int>
)During Item insertion, you will do two operations
a. Update
ITEMSREF
by incrementing theCount
property by one and set the ReturnValues option toUPDATED_NEW
b. Insert Item by calculating the PK and SK
var calculatedPage = (int)(newIdNum / PARTITION_SIZE);
var calculatedItemIndex = (newIdNum - 1) % PARTITION_SIZE;
To fetch a specific page, you should query the table with PK set to
ITEM#<PageIndex>
Example Code:
Finally, to allow for dynamic page sizes, you will have a rule and a decision to make as follows.
Then you would need to change the GetItems method code a bit as follows. The example is of over-fetching
P.S.: