通过全局二级索引查询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错误,说明指定的开始键无效。时间戳是表索引和全局二级索引的范围键,并且属性值对有效(即,表中存在一个值作为索引的散列和范围键传递的项,并且作为索引传递的属性是全局二级索引的散列键)。
是不是我错过了什么,或者这是不可能的?
发布于 2014-02-15 04:44:32
对于亚马逊人来说,这是不可能的:https://forums.aws.amazon.com/thread.jspa?threadID=146102&tstart=0
不过,适用于我的用例的一种变通方法是,只指定一个大于上次检索到的对象的时间戳的RangeKeyCondition。想法是这样的:
Condition hashKeyCondition = new Condition();
hashKeyCondition.withComparisonOperator(ComparisonOperator.EQ).withAttributeValueList(new AttributeValue().withS(hashKeyAttributeValue));
Condition rangeKeyCondition = new Condition();
rangeKeyCondition.withComparisonOperator(ComparisonOperator.GT).withAttributeValueList(new AttributeValue().withN(timestamp.toString()));
Map<String, Condition> keyConditions = new HashMap<String, Condition>();
keyConditions.put(MappedItem.INDEXED_ATTRIBUTE_NAME, hashKeyCondition);
keyConditions.put(MappedItem.TIMESTAMP, rangeKeyCondition);
QueryRequest queryRequest = new QueryRequest();
queryRequest.withTableName(tableName);
queryRequest.withIndexName(MappedItem.INDEX_NAME);
queryRequest.withKeyConditions(keyConditions);
QueryResult result = amazonDynamoDBClient.query(queryRequest);
List<MappedItem> mappedItems = new ArrayList<MappedItem>();
for(Map<String, AttributeValue> item : result.getItems()) {
MappedItem mappedItem = dynamoDBMapper.marshallIntoObject(MappedItem.class, item);
mappedItems.add(mappedItem);
}
return mappedItems;请注意,为了支持DynamoDBMapper类中的受保护方法,marshallIntoObject方法已被弃用,但是如果将来进行升级以破坏映射,那么编写编组程序就足够容易了。
虽然不像使用映射器那样优雅,但它实现了同样的功能。
发布于 2016-08-14 06:29:37
也有同样的问题,刚刚被解决了。:)回答这个问题太晚了,但希望有人能帮上忙。
在查询或扫描具有辅助索引和分页的表时,应在设置ExclusiveStartKey.时包括表和索引的主键(作为键)以及上次求值的值(作为属性值
只需从查询或扫描结果中系统输出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"));发布于 2020-01-01 04:05:08
好吧,我参加聚会来晚了,但我已经搞清楚是怎么回事了。这不是一个bug,它正常工作,但我从未在文档中看到过它。
事实证明,在全局辅助索引中,主索引被用作“决胜者”。也就是说,如果两个对象具有相同的GSIGSI键,则主索引用于在hash+sort中对它们进行排序。这意味着当您查询具有独占起始键的GSI时,您需要GSI索引和主索引,以便从正确的位置开始。
也许这能帮到某个人。我知道它把我难倒了一段时间!
https://stackoverflow.com/questions/21730183
复制相似问题