Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamoDbEnhancedClient: @QueryEnhancedRequest limit parameter is not limiting the search results as expected #1951

Closed
lakshmi-prameela-lanka-wcar opened this issue Jul 13, 2020 · 5 comments
Labels
closed-for-staleness guidance Question that needs advice or information. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days.

Comments

@lakshmi-prameela-lanka-wcar
Copy link

lakshmi-prameela-lanka-wcar commented Jul 13, 2020

Describe the bug

Using aws DynamoDB Java sdk V2, and I would like 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.

Expected Behavior

QueryEnhnacedRequest operation should limit the number of items that it reads

Current Behavior

The limit parameter that we set on QueryEnhancedRequest is giving all the results as per the query condition, thought the limit parameter is set.

https://stackoverflow.com/questions/62417472/limit-method-in-queryenhancedrequest-for-dynamodb-java-v2-sdk-doesnt-limit-as-e

Steps to Reproduce

QueryEnhancedRequest.Builder queryEnhancedRequest = QueryEnhancedRequest.builder()
    .queryConditional(QueryConditional.keyEqualTo(Key.builder().partitionValue(partitionKey).build()))
    .limit(2);

Context

Trying to fetch results from Dyanmodb using DynamoDbEnhancedClient with QueryEnhancedRequest

Your Environment

  • AWS Java SDK version used: SDK version 2.13.52
  • JDK version used:11
  • Operating System and version:Linux 5.0.0-37-generic x86_64
@lakshmi-prameela-lanka-wcar lakshmi-prameela-lanka-wcar added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 13, 2020
@lakshmi-prameela-lanka-wcar lakshmi-prameela-lanka-wcar changed the title QueryEnhancedRequest limit parameter is not limiting the search results as expected DynamoDbEnhancedClient: @QueryEnhancedRequest limit parameter is not limiting the search results as expected Jul 13, 2020
@debora-ito
Copy link
Member

debora-ito commented Jul 15, 2020

Hi @lakshmi-prameela-lanka-wcar the limit in the QueryEnhancedRequest defines the limit of items per page. The SDK v2 by default uses pagination methods that fetches all pages automatically.

If you want to define a limit in the fetched results, you can specify it in the Iterable using the stream method:

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

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

Let us know if that helps.

EDIT: there's other ways to limit the number of results, see comment here: #3226 (comment)

@debora-ito debora-ito added guidance Question that needs advice or information. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days. and removed bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 15, 2020
@github-actions
Copy link

It looks like this issue hasn’t been active in longer than a week. In the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please add a comment to prevent automatic closure, or if the issue is already closed please feel free to reopen it.

@github-actions github-actions bot added closing-soon This issue will close in 4 days unless further comments are made. closed-for-staleness and removed closing-soon This issue will close in 4 days unless further comments are made. labels Jul 22, 2020
@90lantran
Copy link

90lantran commented Oct 13, 2020

Hi @debora-ito ,
Supposed I have a Movie table and a class like this

public class Movie {

public String movieId;

@DynamoDbPartitionKey
@DynamoSecondaryPartitionKey(indexNames= {moviesCreationTimeIndex})
public String getMovieId(){return movieId};

public String movieName;

@DynamoDbSortKey
public String getMovieName(){return movieName};

public String creationTime;

@DynamoDbSecondarySortKey(indexNames = {moviesCreationTimeIndex})
public String getCreationTime(){return creationTime};

}

I am using the enhanced request with LSI instead of table. My code looks something like this.

Iterator<Page<Movie>> movieIterator = moviesCreationTimeIndex.query(queryEnhancedRequest).iterator();

Did you mean this query automtically fetch all items in dynamoDB? Right now, I run into performance issue because some movies have like 10k items and requests takes like 10s.

I actually want to limit like just 100 movies in order of the creationTime which is why I created the LSI.

@allenlipeng47
Copy link

allenlipeng47 commented Dec 31, 2021

@debora-ito For code snippet you presented, where do I get the lastEvaluatedKey through PageIterable?

aws-sdk-java-automation added a commit that referenced this issue Mar 3, 2022
…b6a258c21

Pull request: release <- staging/dabc7d80-a38e-4a21-931b-c74b6a258c21
@molBR
Copy link

molBR commented May 31, 2022

Hey folks!
I just found this issue and as it happens I am in the same boat as @90lantran mentioned.
It is indeed a performance issue and the odd thing is that if you build a raw query such as: Query(hash_key=..., ScanIndexForward=True, limit=1) you get the number of items as limited but if you use the sdk2 library you cannot reproduce the result using the semantically similar function. Shouldn't this be a bug?

Source: https://stackoverflow.com/questions/12809295/nosql-getting-the-latest-values-from-tables-dynamodb-azure-table-storage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-for-staleness guidance Question that needs advice or information. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 10 days.
Projects
None yet
Development

No branches or pull requests

5 participants