Reputation: 804
I am trying to set exclusive start key to my request so that I can have my LastEvaluatedKey afterwards.
The follow exception occurs when I iterate over my query result. Can anyone help me in this regard?
"The provided starting key does not match the range key predicate"
QuerySpec queryExpression = ...
queryExpression = queryExpression.withExclusiveStartKey(OFFSET_PRIMARY_KEY, offsetParts[OFFSET_PRIMARY_KEY_INDEX], OFFSET_SORT_KEY, Long.valueOf( offsetParts[OFFSET_SORT_KEY_INDEX]));
ItemCollection<QueryOutcome> items = table.query(queryExpression);
Iterator<Item> iterator = items.iterator();
iterator.forEachRemaining(m -> { //exception occurs here
...
});
Stack Trace:
com.amazonaws.AmazonServiceException: The provided starting key does not match the range key predicate (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 6EA9NOO079F7VUVV1JP92P05MRVV4KQNSO5AEMVJF66Q9ASUAAJG)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1389)
at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:902)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:607)
at com.amazonaws.http.AmazonHttpClient.doExecute(AmazonHttpClient.java:376)
at com.amazonaws.http.AmazonHttpClient.executeWithTimer(AmazonHttpClient.java:338)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:287)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:1985)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.query(AmazonDynamoDBClient.java:1620)
at com.amazonaws.services.dynamodbv2.document.internal.QueryCollection.firstPage(QueryCollection.java:53)
at com.amazonaws.services.dynamodbv2.document.internal.PageIterator.next(PageIterator.java:45)
at com.amazonaws.services.dynamodbv2.document.internal.PageIterator.next(PageIterator.java:25)
at java.lang.Iterable.forEach(Iterable.java:74)
at com.my.com.services.database.DynamoDatabaseRepository.fetchMessageList(DynamoDatabaseRepository.java:675)
at service.DatabaseServiceTest.accc(DatabaseServiceTest.java:106)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:130)
Upvotes: 4
Views: 9400
Reputation: 349
The “The provided starting key does not match the range key predicate” means that DynamoDB cannot hit the Item that ExclusiveStartKey represents by your query condition.So when you use queryPage api, you'd better not change the query condition until you find the last item; and you'd better not update the item that may be included in your pagination result.
Hope this answer can help someone.
Upvotes: 1
Reputation: 4284
This can happen if the row represented by the ExclusiveStartKey that you are passing no longer appears in the dynamodb query result.
Consider the case below:
Your Primary partition key is named "message_id" Your Primary sort key is named "message_timestamp" Your ExclusiveStartKey value is: '{"message_id":"3483b80a358cf60decfccc329bd5e72e","message_timestamp":1481114525}' You are using KeyConditionExpression as: Key("message_id").eq("3483b80a358cf60decfccc329bd5e72e") & Key("message_timestamp").gt(1481202107) Since the message_timestamp in KeyConditionExpression is greater than the corresponding value in ExclusiveStartKey, dynamodb will throw the error that you mentioned.
Upvotes: 0