user3742622
user3742622

Reputation: 1077

AWS DynamoDB get multiple items by String an range with Java

I am trying to understand a DynamoDB. I created a table and put some values there. But I completely don't understand how to get the item. And any docs which I found don't help.

I have a table with Primary partition key - channelID (String) and Primary sort key timestamp (Number). There are a few objects on my Primary partition key but each object by Primary partition key + Primary sort key is uniq. Everything I tried didn't work for me. I need just get 10 items with same channelID with order by timestamp desc.

My Java code:

String partitionKey = channelID;

            DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);

            NewsEntityItem newsKey = new NewsEntityItem();
            newsKey.setChannelID(partitionKey);

            DynamoDBQueryExpression<NewsEntityItem> queryExpression = new DynamoDBQueryExpression<NewsEntityItem>()
                    .withHashKeyValues(newsKey);

            List<NewsEntityItem> theResult = mapper.query(NewsEntityItem.class, queryExpression);

            for (NewsEntityItem reply : theResult) {
                System.out.print(reply.toString());
            }

Upvotes: 1

Views: 2565

Answers (2)

Chen Harel
Chen Harel

Reputation: 10062

In order to get the results backwards you should use the withScanIndexForward(false).

In order to get only part of the results it is tricky to use the withLimit(5) even though this is what you probably looked at.

As you'll see in the limit documentation - setting limit is not exactly what you want, so you should also look at queryPage.

To conclude, use either and count 5 elements in Java code before breaking.

Upvotes: 0

AmitGaur
AmitGaur

Reputation: 51

Here is an example from the AWS DynamoDB docs on how to use the Mapper api, look at the FindRepliesPostedWithinTimePeriod or FindRepliesInLast15Days methods http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaQueryScanORMModelExample.html

Upvotes: 0

Related Questions