0

I need to implement a Query operation on DynamoDB. Right now I'm doing it by giving the HashKey and then filtering out the results according to my conditions on non-key attributes.

This is what I'm doing :

MusicData hashKey = new MusicData();
hashKey.setID(singer); 

DynamoDBQueryExpression<MusicData> queryExpression = new DynamoDBQueryExpression<MusicData>().withHashKeyValues(hashKey);

List<MusicData> queryResult = mapper.query(MusicData.class, queryExpression);

for (MusicData musicData : queryResult) {
    if( my condtions ) {
        do something;   
    }
}

What I'm trying to do is to be able to do something like this :

MusicData hashKey = new MusicData();
hashKey.setID(singer); 
hashKey.setAlbum(sampleAlbum);
hashKey.setSinger(duration);

DynamoDBQueryExpression<MusicData> queryExpression = new DynamoDBQueryExpression<MusicData>().withHashKeyValues(hashKey);

List<MusicData> queryResult = mapper.query(MusicData.class, queryExpression);

for (MusicData musicData : queryResult) {
    if( my condtions ) {
        do something;   
    }
}

And get results already filtered out. Is there a way to do this in DynamoDB?

1 Answer 1

1

Yes, you can ask DynamoDB to perform filtering of queries before it returns results. However, you will still incur the 'cost' of reading these items even though they are not returned to your client. This is still a good practice as it will eliminate unnecessary transfer of items over the network.

To do this you will call additional methods on your DynamoDBQueryExpression object, specifically withFilterExpression and addExpressionAttributeNamesEntry / addExpressionAttributeValuesEntry to complete the expression.

Without the specific example of what type of conditions you want to apply it is hard to give an example, but depending on how simple your condition you want to apply is you could contain it in just the withFilterExpression method.

DynamoDBQueryExpression<MusicData> queryExpression = new DynamoDBQueryExpression<MusicData>().withHashKeyValues(hashKey).withFilterExpression("foo > 10");
Sign up to request clarification or add additional context in comments.

2 Comments

Okay, I was looking for a way to extract only those items according to my condition, rather than getting all Items and then filtering out on my conditions.
If your condition is only on the range key that is possible using a different method, but without the specifics what I described above is exactly what you are asking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.