具有独占起始键的DynamoDB全局二级索引 [英] DynamoDB Global Secondary Index with Exclusive Start Key

查看:35
本文介绍了具有独占起始键的DynamoDB全局二级索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过全局辅助索引查询DynamoDB表时是否可以指定独占起始键?

我使用的是aws-java-sdk版本1.6.10,并使用QueryExpressionDynamoDBMapper执行查询。以下是我尝试做的要点:

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错误,指出指定的开始键无效。时间戳是表索引和全局二级索引的范围键,并且属性值对有效(即,表中有一个项目的值作为索引的散列和范围键传递,而作为索引传递的属性是全局二级索引的散列键)。

是我遗漏了什么,还是这是不可能的?

推荐答案

对于亚马逊人来说,这是不可能的: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方法受到抨击,但如果将来升级以中断映射,编写封送处理程序就足够容易了。

不像使用映射器那样优雅,但它可以实现相同的功能。

这篇关于具有独占起始键的DynamoDB全局二级索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆