I want to scan my Dynamo db table with pagination applied to it. In my request I want to send the number from where I want pagination to get start. Say, e.g. I am sending request with start = 3 and limit = 10, where start is I want scan to start with third item in the table and limit is upto 10 items. Limit however I can implement with .withLimit() method(I am using java). I followed this aws document. Following is the code of what I want to achieve:
<Map<String, AttributeValue>> mapList = new ArrayList<>();
AmazonDynamoDB client =AmazonDynamoDBClientBuilder.standard().build();
Gson gson = new GsonBuilder().serializeNulls().create();
Map<String, AttributeValue> expressionAttributeValues = new
HashMap<String,AttributeValue>();
expressionAttributeValues.put(":name",
newAttributeValue().withS(name));
List<ResponseDomain> domainList = new ArrayList<>();
ResponseDomain responseDomain = null;
//lastKeyEvaluated = start
Map<String, AttributeValue> lastKeyEvaluated = null;
do {
ScanRequest scanRequest = new
ScanRequest().withTableName(STUDENT_TABLE)
.withProjectionExpression("studentId, studentName")
.withFilterExpression("begins_with(studentName, :name)")
.withExpressionAttributeValues(expressionAttributeValues).
withExclusiveStartKey(lastKeyEvaluated);
ScanResult result = client.scan(scanRequest);
for (Map<String, AttributeValue> item : result.getItems()) {
responseDomain = gson.fromJson(gson.toJson(item),
ResponseDomain.class);
domainList.add(responseDomain);
} lastKeyEvaluated = result.getLastEvaluatedKey();
} while (lastKeyEvaluated!= null);
//lastKeyEvaluated = size
return responseDomain;
In the above code I am stuck at 3 places:
- How can I set lastKeyEvaluated as my start value i.e 3
- In the while condition how can I specify my limit i.e 10
- When I try to map item from Json to my domain class, I encounter error.
Am I misinterpreting the concept of pagination in dynamodb or doing something wrong in the code. Any guidance will be highly appreciated as I am a newbie.