DynamoDB withLimit clause with DynamoDBMapper.query

I use DynamoDBMapper for a class, say, "User" (username is the primary key), which has a field that says "Status". This is a Hash + Range key table, and every time a user’s status changes (changes rarely occur), we add a new record to the table along with a time stamp (which is a range key). To get the current status, this is what I do:

    DynamoDBQueryExpression expr =
        new DynamoDBQueryExpression(new AttributeValue().withS(userName))
               .withScanIndexForward(false).withLimit(1);

    PaginatedQueryList<User> result = 
                this.getMapper().query(User.class, expr);

    if(result == null || result.size() == 0) {
        return null;
    }

    for(final User user : result) {
        System.out.println(user.getStatus());
    }

This for some reason prints all the statuses that the user has had so far. I set it scanIndexForwardto false to be in descending order, and I imposed a restriction 1. I expect this to return the last single entry in the table for this username.

, , , , 1. :

final String currentStatus = result.get(0).getStatus();

, , , withLimit , - ?

+4
2

2013 AWS .

Amazon queryPage.

, , , API, Page.

, Gitub .

+6

, , withLimit , . :

  • DynamoDBQueryExpressionExpression withLimit (SOME_NUMBER);
  • queryPage :

    QueryResultPage<T> page = dynamoDBMapper.queryPage(T.class, queryExpression);

  • page.getResults(), .

.

. withLimit (. ) : http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBQueryExpression.html#withLimit(java.lang.Integer)

-1

Source: https://habr.com/ru/post/1525763/


All Articles