我下载了AWS Java SDK的1.4.5版本,但我无法在DynamoDB表上迁移查询。这是一个简单的散列+范围查询。
V1工作正常:
Condition c = new Condition().withComparisonOperator(ComparisonOperator.LT)
.withAttributeValueList(new AttributeValue(new Date().toString()));
DynamoDBQueryExpression q = new DynamoDBQueryExpression(new AttributeValue("john")).withRangeKeyCondition(c);
api的V2似乎有点不同。方法签名更改要求将代码重写为:
Condition c = new Condition().withComparisonOperator(ComparisonOperator.LT)
.withAttributeValueList(new AttributeValue(new Date().toString()));
DynamoDBQueryExpression q = new DynamoDBQueryExpression()
.withIndexName("user")
.withHashKeyValues("john")
.withRangeKeyCondition("timestamp", c);
AWS SDK会抛出异常:
The range key(timestamp) in the query is the primary key of the table, not the range key of index(user)
是否有人有一个代码示例说明如何使用新的v2 api为DynamoDB执行查询?
答案 0 :(得分:1)
很抱歉,如果这已经很晚了,但是如果它对您或其他任何人有帮助,那么您的代码问题就在于您致电.withIndexName("john")
时。
使用withIndexName()
使用本地或全局二级索引进行查询;不适用于使用表的散列键或散列键和范围键的查询。当您要执行的操作是搜索散列密钥为"john"
的项目时,DynamoDB正在查找名为"john"
的本地或全局辅助索引。