有没有人知道我可能会缺少什么?
表定义:
{ AttributeDeFinitions:[ { AttributeName:timestamp,AttributeType:N },{ AttributeName:url,AttributeType:S } ],TableName:SitePageIndexed,KeySchema:[ { AttributeName:url,KeyType:HASH } ],TableStatus:ACTIVE,CreationDateTime: Mon May 12 18:45:57 EDT 2014,ProvisionedThroughput:{ NumberOfDecreasesToday:0,ReadCapacityUnits:8,WriteCapacityUnits:4 },TableSizeBytes:0,ItemCount:0,GlobalSecondaryIndexes:[ { IndexName:TimestampIndex,KeySchema:[ { AttributeName:timestamp,KeyType:HASH } ],Projection:{ ProjectionType:ALL,},IndexStatus:ACTIVE,ProvisionedThroughput:{ NumberOfDecreasesToday:0,WriteCapacityUnits:4 },IndexSizeBytes:0,ItemCount:0 } ] }
码
Condition condition1 = new Condition().withComparisonoperator(Comparisonoperator.GE).withAttributeValueList(new AttributeValue().withN(Long.toString(start))); DynamoDBQueryExpression<SitePageIndexed> exp = new DynamoDBQueryExpression<SitePageIndexed>().withRangeKeyCondition("timestamp",condition1); exp.setScanIndexForward(true); exp.setLimit(100); exp.setIndexName("TimestampIndex"); PaginatedQueryList<SitePageIndexed> queryList = client.query(SitePageIndexed.class,exp);
解决方法
All I’m trying to do is to get all items that have a timestamp greater than a value without considering the key.
这不是Amazon DynamoDB上的全局二级索引(GSI)的工作方式.要查询GSI,您必须为其散列键指定一个值,然后您可以按范围键进行过滤/排序 – 就像您使用主键一样.这正是异常试图告诉您的内容,以及您在documentation page for the Query
API上会发现的内容:
A Query operation directly accesses items from a table using the table primary key,or from an index using the index key. You must provide a specific hash key value.
可以将GSI视为另一个与主键几乎完全相同的键(主要区别在于它是异步更新的,并且您只能在GSI上执行最终一致的读取).
有关创建GSI时的指南和最佳实践,请参阅Amazon DynamoDB全局二级索引文档页面:http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html
实现所需功能的一种可能方法是将虚拟属性约束为有限的一小组可能值,在该虚拟属性上创建带有散列键的GSI,并在时间戳上创建范围键.查询时,您需要为虚拟哈希键属性上的每个可能值发出一个Query API调用,然后在应用程序上合并结果.通过将dummy属性约束为单例(即,具有单个元素的Set,即常量值),您只能发送一个Query API调用并直接获得结果数据集 – 但请记住,这会导致您遇到与热分区相关的问题,您可能会遇到性能问题!再次,请参阅上面链接的文档以了解最佳实践和一些模式.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。