提供的起始键与范围键断言不匹配。

huangapple go评论85阅读模式
英文:

The provided starting key does not match the range key predicate

问题

以下是您要翻译的内容:

"I have a DynamoDB table where:

PK: String

and a GSI on this table where PK and SK are attributes of the original table:

PK: String

SK: String
(actually the SK it's the String representation of a Java Instant)

For a test, I'm trying to crawl over the pages returned by the queryPage method.
I've created 4 items and set the page size to 2.

When retrieving the first page (2 items) I'm doing:

DynamoDbAddress key = a new DynamoDbAddress();
key.setProvider(DATA_PROVIDER);

return dbMapper.queryPage(DynamoDbAddress.class, a new DynamoDBQueryExpression<DynamoDbAddress>()
.withLimit(pageSize)
.withIndexName(TABLE_GSI)
.withHashKeyValues(key)
.withRangeKeyCondition("Time", new Condition().withComparisonOperator(ComparisonOperator.LE)
.withAttributeValueList(new AttributeValue(Instant.now().minus(26, ChronoUnit.DAYS).toString())))
)
.getResults();

This snippet is working fine and retrieves 2 elements as expected.
When retrieving the second page (the final 2 items), I'm doing:

DynamoDbAddress key = a DynamoDbAddress();
key.setProvider(DATA_PROVIDER);

return dbMapper.queryPage(DynamoDbAddress.class, a DynamoDBQueryExpression<DynamoDbAddress>()
.withLimit(pageSize)
.withIndexName(TABLE_GSI)
.withHashKeyValues(key)
.withRangeKeyCondition("Time", new Condition().withComparisonOperator(ComparisonOperator.LE)
.withAttributeValueList(new AttributeValue(Instant.now().minus(26, ChronoUnit.DAYS).toString())))
.withExclusiveStartKey(Map.of(
"Provider", new AttributeValue().withS(lastProcessed.getProvider().toString()),
"Time", new AttributeValue().withS(lastProcessed.getTime().toString()),
"PK", new AttributeValue().withS(lastProcessed.getPartitionKey()))))
.getResults();

However, I'm getting the following error:

The provided starting key does not match the range key predicate

My interpretation of the error is that "the lastProcessed element does not match the condition expressed in the range key". However, this looks a little absurd to me, as it was retrieved by the very same query in the snippet executed a second before.

I've also verified the test data match the key condition"

英文:

I have a DynamoDB table where:

PK: String

and a GSI on this table where PK and SK are attributes of the original table:

PK: String

SK: String 

(actually the SK it's the String representation of a Java Instant)

For a test, I'm trying to crawl over the pages returned by the queryPage method.
I've created 4 items and set the page size to 2.

When retrieving the first page (2 items) I'm doing:

DynamoDbAddress key = new DynamoDbAddress();
key.setProvider(DATA_PROVIDER);

return dbMapper.queryPage(DynamoDbAddress.class, new DynamoDBQueryExpression&lt;DynamoDbAddress&gt;()
                .withLimit(pageSize)
                .withIndexName(TABLE_GSI)
                .withHashKeyValues(key)
                .withRangeKeyCondition(&quot;Time&quot;, new Condition().withComparisonOperator(ComparisonOperator.LE)
                        .withAttributeValueList(new AttributeValue(Instant.now().minus(26, ChronoUnit.DAYS).toString())))
                )
        .getResults();

This snippet is working fine and retrieves 2 elements as expected.
When retrieving the second page (the final 2 items), I'm doing:

    DynamoDbAddress key = new DynamoDbAddress();
    key.setProvider(DATA_PROVIDER);

    return dbMapper.queryPage(DynamoDbAddress.class, new DynamoDBQueryExpression&lt;DynamoDbAddress&gt;()
            .withLimit(pageSize)
            .withIndexName(TABLE_GSI)
            .withHashKeyValues(key)
            .withRangeKeyCondition(&quot;Time&quot;, new Condition().withComparisonOperator(ComparisonOperator.LE)
                    .withAttributeValueList(new AttributeValue(Instant.now().minus(26, ChronoUnit.DAYS).toString())))
            .withExclusiveStartKey(Map.of(
                            &quot;Provider&quot;, new AttributeValue().withS(lastProcessed.getProvider().toString()),
                            &quot;Time&quot;, new AttributeValue().withS(lastProcessed.getTime().toString()),
                            &quot;PK&quot;, new AttributeValue().withS(lastProcessed.getPartitionKey()))))
.getResults();

However, I'm getting the following error:

The provided starting key does not match the range key predicate

My interpretation of the error is that "the lastProcessed element does not match the condition expressed in the range key". However, this looks a little absurd to me, as it was retrieved by the very same query in the snippet executed a second before.

I've also verified the test data match the key condition

答案1

得分: 1

为什么要手动重建ExclusiveStartKey?为了避免像您所见到的问题,您应该调用getLastEvaluatedKey()

    DynamoDbAddress key = new DynamoDbAddress();
    key.setProvider(DATA_PROVIDER);

    return dbMapper.queryPage(DynamoDbAddress.class, new DynamoDBQueryExpression<DynamoDbAddress>()
            .withLimit(pageSize)
            .withIndexName(TABLE_GSI)
            .withHashKeyValues(key)
            .withRangeKeyCondition("Time", new Condition().withComparisonOperator(ComparisonOperator.LE)
                    .withAttributeValueList(new AttributeValue(Instant.now().minus(26, ChronoUnit.DAYS).toString())))
            .withExclusiveStartKey(lastResult.getEvaluatedKey())
            .getResults();
英文:

Why are you trying to manually reconstruct the ExclusiveStartKey? To avoid issues, like the one you are seeing, you should call getLastEvaluatedKey()

    DynamoDbAddress key = new DynamoDbAddress();
    key.setProvider(DATA_PROVIDER);

    return dbMapper.queryPage(DynamoDbAddress.class, new DynamoDBQueryExpression&lt;DynamoDbAddress&gt;()
            .withLimit(pageSize)
            .withIndexName(TABLE_GSI)
            .withHashKeyValues(key)
            .withRangeKeyCondition(&quot;Time&quot;, new Condition().withComparisonOperator(ComparisonOperator.LE)
                    .withAttributeValueList(new AttributeValue(Instant.now().minus(26, ChronoUnit.DAYS).toString())))
            .withExclusiveStartKey(lastResult.getEvaluatedKey())
.getResults();

huangapple
  • 本文由 发表于 2023年3月7日 00:32:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定