通过全局二级索引查询DynamoDB表时是否可以指定独占起始键?
我正在使用 aws-java-sdk 版本 1.6.10 并使用 QueryExpression
和 DynamoDBMapper
执行查询。这是我正在尝试做的事情的要点:
MappedItem key = new MappedItem();
item.setIndexedAttribute(attributeValue);
Map<String, AttributeValue> exclusiveStartKey = new HashMap<String, AttributeValue>();
exclusiveStartKey.put(MappedItem.INDEXED_ATTRIBUTE_NAME, new AttributeValue().withS(attributeValue));
exclusiveStartKey.put(MappedItem.TIMESTAMP, new AttributeValue().withN(startTimestamp.toString()));
DynamoDBQueryExpression<MappedItem> queryExpression = new DynamoDBQueryExpression<MappedItem>();
queryExpression.withIndexName(MappedItem.INDEX_NAME);
queryExpression.withConsistentRead(Boolean.FALSE);
queryExpression.withHashKeyValues(key);
queryExpression.setLimit(maxResults * 2);
queryExpression.setExclusiveStartKey(exclusiveStartKey);
这会导致 400 错误,指出指定的开始键无效。 TIMESTAMP 是表索引和全局二级索引的范围键,属性值对有效(即表中有一个项目,其值作为索引的散列和范围键传递,并且属性作为索引传递的是全局二级索引的哈希键)。
我是否遗漏了什么或者这是不可能的?
最佳答案
遇到了同样的问题,刚刚解决了。 :) 来不及回答这个问题,但希望有人会有所帮助。
当您使用二级索引和分页查询或扫描表时,您应该包括表和索引(作为键)的主键,以及最后评估的值(作为属性值)当您设置 ExclusiveStartKey 时。
只需从查询或扫描结果中 Sysout LastEvaluatedKey 即可查看格式。
// let's just assume that we have a table to store details of products
Map<String, AttributeValue> exclusiveStartKey = new HashMap<String, AttributeValue>();
// primary key of the table
exclusiveStartKey.put("productId", new AttributeValue().withS("xxxx"));
exclusiveStartKey.put("produtSize", new AttributeValue().withS("XL"));
// primary key of the index
exclusiveStartKey.put("categoryId", new AttributeValue().withS("xx01"));
exclusiveStartKey.put("subCategoryId", new AttributeValue().withN("1"));
关于java - 具有独占启动键的 DynamoDB 全局二级索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21730183/