Mike Smith
Mike Smith

Reputation: 87

DynamoDBMapper v2 exception

I downloaded version 1.4.5 of the AWS Java SDK and I'm having trouble migrating a query on a DynamoDB table. This is a simple hash + range query.

V1 works ok:

Condition c = new Condition().withComparisonOperator(ComparisonOperator.LT)
                         .withAttributeValueList(new AttributeValue(new Date().toString()));
DynamoDBQueryExpression q = new DynamoDBQueryExpression(new  AttributeValue("john")).withRangeKeyCondition(c);

V2 of the api seems to work a little different. Method signature changes require the code to be re-written as:

Condition c = new Condition().withComparisonOperator(ComparisonOperator.LT)
                                 .withAttributeValueList(new AttributeValue(new Date().toString()));
DynamoDBQueryExpression q = new DynamoDBQueryExpression()
                                    .withIndexName("user")
                                    .withHashKeyValues("john")
                                    .withRangeKeyCondition("timestamp", c);

The AWS SDK throws an exception:

The range key(timestamp) in the query is the primary key of the table, not the range key of index(user)                                        

Does any one have a code sample illustrating how to execute a query with the new v2 api for DynamoDB?

Upvotes: 0

Views: 2119

Answers (1)

rpmartz
rpmartz

Reputation: 3809

Sorry if this is late, but if it helps you or anyone else, the problem with your code is when you call .withIndexName("john").

You use withIndexName() to query using a local or global secondary index; not for queries using the hash key or hash key and range key of the table. DynamoDB is looking for a local or global secondary index named "john" when what you want to do is search for items with a hash key of "john".

Upvotes: 1

Related Questions