java 如何从 DynamoDBMapper query() 的分页结果中检索所有项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10804282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I retrieve all items from the paginated result of a DynamoDBMapper query()?
提问by GG.
I am trying to query DynamoDB with help of DynamoDBMapperin Java with both hashKey and rangeKey. But I am not getting all results, it returns only some part of it. My code looks like:
我想查询DynamoDB与帮助DynamoDBMapper在Java中既hashKey和rangeKey。但我没有得到所有结果,它只返回其中的一部分。我的代码看起来像:
queryDynamoDb() {
Condition rangeKeyCondition = new Condition()
.withComparisonOperator(ComparisonOperator.GT.toString())
.withAttributeValueList(new AttributeValue().withS("0"));
DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression(
new AttributeValue().withS(prefKey));
queryExpression.setRangeKeyCondition(rangeKeyCondition);
List<MyObj> myobjs = mapper.query(MyObj.class, queryExpression);
return myobjs;
}
MyObj
is properly annotated with DynamoDB annotations. So I am able to save the objects, but retrieval returns a partial result only.
MyObj
已使用 DynamoDB 注释正确注释。所以我可以保存对象,但检索只返回部分结果。
The documentation of querywithin DynamoDBMappersays:
的文件查询中DynamoDBMapper说:
The query method returns the "lazy-loaded" collection. That is, initially it returns only one page of results. It makes a service call for the next page when needed.
查询方法返回“延迟加载”集合。也就是说,最初它只返回一页结果。它会在需要时为下一页发出服务呼叫。
Now the question is, how to tell the mapper to make a service call or that a page is needed, so it loads all pages (effectively all entries)?
现在的问题是,如何告诉映射器进行服务调用或需要一个页面,以便加载所有页面(实际上是所有条目)?
采纳答案by Steffen Opel
The Java code snippet within the Amazon DynamoDBdocumentation for the DynamoDBMapper Classis a bit unfortunate here (though technologically correct), the AWS SDK for JavaAPI documentation for Class DynamoDBMapperis (naturally) more precise in this regard, see method query():
在中的Java代码片断亚马逊DynamoDB文档中的DynamoDBMapper类是有点可惜这里(虽然在技术上是正确的),在AWS SDK的JavaAPI文档类DynamoDBMapper是(自然)在这方面更精确,请参阅方法查询() :
public <T> PaginatedQueryList<T> query(Class<T> clazz,
DynamoDBQueryExpression queryExpression)
So the returned type is actually a Class PaginatedQueryList:
所以返回的类型实际上是一个PaginatedQueryList 类:
Implementation of the List interface that represents the results from a query in AWS DynamoDB. Paginated results are loaded on demandwhen the user executes an operation that requires them. Some operations, such as size(), must fetch the entire list, but results are lazily fetched page by page when possible. [emphasize mine]
表示 AWS DynamoDB 中查询结果的 List 接口的实现。分页结果在用户执行需要它们的操作时按需加载。某些操作,例如 size(),必须获取整个 list,但在可能的情况下,会逐页延迟获取结果。[强调我的]
That is, you really do not need to explicitly load anything during normal usage, insofar it is implicitly taken care of by the lazy-loading implementation of PaginatedQueryList<T>
; however, if so desired for whatever reason, you can trigger it by operations requiring access to the entire collection, with the explicitly mentioned size()method being one of them apparently.
也就是说,您真的不需要在正常使用期间显式加载任何内容,只要它由 的延迟加载实现隐式处理PaginatedQueryList<T>
;但是,如果出于某种原因需要,您可以通过需要访问整个集合的操作来触发它,明确提到的size()方法显然是其中之一。