3

I am trying to use the DynamoDBScanExpression withLimit of 1 using Java aws-sdk version 1.11.140

Even if I use .withLimit(1) i.e.

List<DomainObject> result = mapper.scan(new DynamoDBScanExpression().withLimit(1)); 

returns me list of all entries i.e. 7. Am I doing something wrong?

P.S. I tried using cli for this query and

aws dynamodb scan --table-name auditlog --limit 1 --endpoint-url http://localhost:8000 

returns me just 1 result.

Yash
  • 85
  • 1
  • 10

1 Answers1

0

DynamoDBMapper.scan will return a PaginatedScanList - Paginated results are loaded on demand when the user executes an operation that requires them. Some operations, such as size(), must fetch the entire list, but results are lazily fetched page by page when possible.

Hence, The limit parameter set on DynamoDBScanExpression is the maximum number of items to be fetched per page.

So in your case, a PaginatedList is returned and when you do PaginatedList.size it attempts to load all items from Dynamodb, under the hood the items were loaded 1 per page (each page is a fetch request to DynamoDb) till it get's to the end of the PaginatedList.

Since you're only interested in the first result, a good way to get that without fetching all the 7 items from Dynamo would be :

    Iterator it = mapper.scan(DomainObject.class, new DynamoDBScanExpression().withLimit(1)).iterator();
    if ( it.hasNext() ) {
        DomainObject dob = (DomainObject) it.next();
    }

With the above code, only the first item will fetched from dynamodb.

The take away is that : The limit parameter in DynamoDBQueryExpression is used in the pagination purpose only. It's a limit on the amount of items per page not a limit on the number of pages that can be requested.