Partha Mahajani
Partha Mahajani

Reputation: 71

Limit method in QueryEnhancedRequest for DynamoDB Java v2 sdk doesn't limit as expected

I'm using the v2 aws DynamoDV Java sdk, and I want to limit the number of results that are returned when querying by the partition key (code snippet below), but the code below returns the full set of items back.

The java docs say "Note:The limit does not refer to the number of items to return, but how many items the database should evaluate while executing the query. Use limit together with Page.lastEvaluatedKey() and exclusiveStartKey in subsequent query calls to evaluate limit items per call." which seems to support the behavior I'm seeing.

However, How to set limit of matching items returned by DynamoDB using Java? has a solution using the .withMaxResultSize method in an earlier version of the sdk.

Does the java dynamodb v2 skd have something similar, or will I have to limit the result set manually? Code looks like:

QueryConditional conditional = QueryConditional.keyEqualTo(
                Key.builder()
                        .partitionValue(jobId)
                        .build()
        );
QueryEnhancedRequest request = QueryEnhancedRequest.builder()
                .queryConditional(conditional)
                .limit(1)
                .scanIndexForward(false)
                .build();

Upvotes: 7

Views: 9192

Answers (1)

Gopal Shukla
Gopal Shukla

Reputation: 167

Please read https://github.com/aws/aws-sdk-java-v2/issues/1951

You need to limit the number of pages to be returned from iterable. Example

PageIterable<MyMovie> myMovie = moviesTable.query(queryEnhancedRequest);

myMovie.items()
        .stream()
        .limit(2)
        .forEach(content -> System.out.println(" Movie: %s (%s) \n", content.title, content.year));

This will fetch 2 pages and each page will have 1 item if you set limit(1) in the queryEnhancedRequest

Upvotes: 4

Related Questions