如何使用 Java 设置 DynamoDB 返回的匹配项的限制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31644605/
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 to set limit of matching items returned by DynamoDB using Java?
提问by TOP
In my Android app, I want to query data from DynamoDB. There will be a thousand matching items, but I just want to get only the first 10 of them. I don't know how to set this limit. I found these lines in documentation:
在我的 Android 应用程序中,我想从 DynamoDB 查询数据。会有一千个匹配项,但我只想得到其中的前 10 个。我不知道如何设置这个限制。我在文档中找到了这些行:
The DynamoDB Query and Scan APIs allow a Limit value to restrict the size of the results.
DynamoDB 查询和扫描 API 允许使用限制值来限制结果的大小。
In a request, set the Limit parameter to the number of items that you want DynamoDB to process before returning results.
在请求中,将 Limit 参数设置为您希望 DynamoDB 在返回结果之前处理的项目数。
In a response, DynamoDB returns all the matching results within the scope of the Limit value. For example, if you issue a Query or a Scan request with a Limit value of 6 and without a filter expression, DynamoDB returns the first six items in the table that match the specified key conditions in the request (or just the first six items in the case of a Scan with no filter). If you also supply a FilterExpression value, DynamoDB will return the items in the first six that also match the filter requirements (the number of results returned will be less than or equal to 6).
在响应中,DynamoDB 返回限制值范围内的所有匹配结果。例如,如果您发出限制值为 6 且没有过滤器表达式的查询或扫描请求,DynamoDB 将返回表中与请求中指定的键条件匹配的前六项(或仅返回表中的前六项)没有过滤器的扫描的情况)。如果您还提供 FilterExpression 值,DynamoDB 将返回前六个中也符合过滤器要求的项目(返回的结果数将小于或等于 6)。
But I cannot find the way to set limit for response. I found the method SetCount of QueryResult:
但是我找不到设置响应限制的方法。我找到了 QueryResult 的 SetCount 方法:
QueryResult result2 = dynamodb.query(request);
result2.setCount(5);
It said that: then Count is the number of items returned after the filter was applied
它说:then Count 是应用过滤器后返回的项目数
But I think it is not what I want. Because DynamoDb still returns all the matching items before calling setCount. Can any one help me?
但我认为这不是我想要的。因为 DynamoDb 在调用 setCount 之前仍然返回所有匹配项。谁能帮我?
采纳答案by JaredHatfield
You need to apply the limit as part of the request that you send to the API, not on the response.
您需要将限制应用为发送到 API 的请求的一部分,而不是响应。
I assume the request object you are submitting to the dynamodb object is a QuerySpec. What you will want to do is is call withMaxResultSizeto pass in the limit you want applied before the query is run against the API.
我假设您提交给 dynamodb 对象的请求对象是QuerySpec。您要做的是调用withMaxResultSize以在针对 API 运行查询之前传入您想要应用的限制。
However, as you mentioned in your question you need to make sure you understand the behavior of limit as described in the DynamoDB documentation on Limits:
但是,正如您在问题中提到的,您需要确保了解有关Limits的 DynamoDB 文档中描述的 limit 行为:
In a response, DynamoDB returns all the matching results within the scope of the Limit value. For example, if you issue a Query or a Scan request with a Limit value of 6 and without a filter expression, DynamoDB returns the first six items in the table that match the specified key conditions in the request (or just the first six items in the case of a Scan with no filter). If you also supply a FilterExpression value, DynamoDB will return the items in the first sixthat also match the filter requirements (the number of results returned will be less than or equal to 6).
在响应中,DynamoDB 返回限制值范围内的所有匹配结果。例如,如果您发出限制值为 6 且没有过滤器表达式的查询或扫描请求,DynamoDB 将返回表中与请求中指定的键条件匹配的前六项(或仅返回表中的前六项)没有过滤器的扫描的情况)。如果您还提供 FilterExpression 值,DynamoDB 将返回前六个中也符合过滤器要求的项目(返回的结果数将小于或等于 6)。
What this means is that if you are not using a FilterExpression you are likely fine. However, if you are filtering the results you will likely receive fewer than your limit because the limit is technically not the number of results to return rather the number of items DynamoDB could potentially return.
这意味着如果您不使用 FilterExpression,您可能会没事。但是,如果您过滤结果,您收到的结果可能会少于您的限制,因为从技术上讲,限制不是要返回的结果数,而是 DynamoDB 可能返回的项目数。
It sounds like you are asking for a way to have DynamoDB limit the number of results it returns to an exact number while having a FilterExpression applied. Unfortunately this is currently not possible with DynamoDB.
听起来您正在寻求一种方法,让 DynamoDB 在应用 FilterExpression 的同时将其返回的结果数量限制为一个确切的数字。不幸的是,DynamoDB 目前无法做到这一点。
回答by maestr0
This is how you can limit a query result using aws-java-sdk ver 1.10.61
这是使用 aws-java-sdk ver 1.10.61 限制查询结果的方法
import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsyncClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.ItemCollection;
import com.amazonaws.services.dynamodbv2.document.QueryOutcome;
import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
AWSCredentialsProvider provider = new DefaultAWSCredentialsProviderChain();
AmazonDynamoDBAsyncClient client = new AmazonDynamoDBAsyncClient(provider);
Region region = Region.getRegion(Regions.fromName(DYNAMODB_REGION));
client.setRegion(region);
dynamoDB = new DynamoDB(client);
QuerySpec querySpec = new QuerySpec();
/* MAX 1 item */
querySpec.setMaxResultSize(1);
querySpec.withHashKey("myPartitionKeyName", partitionKeyValue);
ItemCollection<QueryOutcome> query = dynamoDB.getTable(DYNAMODB_TABLE).query(querySpec);