无法在DynamoDB上设置独占启动键

2020年9月15日 27点热度 0条评论

我正在尝试为请求设置独占开始键,以便以后可以使用LastEvaluatedKey。

当我遍历查询结果时,将发生以下异常。有人可以在这方面帮助我吗?

“提供的开始键与范围键谓词不匹配”

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
                                ...
                            });

堆栈跟踪:

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)

解决方案如下:

如果您要传递的ExclusiveStartKey所代表的行不再出现在dynamodb查询结果中,则可能发生这种情况。

考虑以下情况:

您的主分区键名为“message_id”
您的主排序键名为“message_timestamp”
您的ExclusiveStartKey值为:'{“message_id”:“3483b80a358cf60decfccc329bd5e72e”,“message_timestamp”:1481114525}'
您将KeyConditionExpression用作:Key(“message_id”)。eq(“3483b80a358cf60decfccc329bd5e72e”)&Key(“message_timestamp”)。gt(1481202107)

由于KeyConditionExpression中的message_timestamp大于ExclusiveStartKey中的相应值,因此dynamodb将引发您提到的错误。