如何使用 Java 设置 DynamoDB 返回的匹配项的限制?

How to set limit of matching items returned by DynamoDB using Java?

在我的 Android 应用程序中,我想从 DynamoDB 查询数据。将有一千个匹配项目,但我只想获得其中的前 10 个。我不知道如何设置这个限制。我在文档中找到了这些行:

DynamoDB 查询和扫描 API 允许使用限制值来限制结果的大小。

在请求中,将限制参数设置为您希望 DynamoDB 在 return 获取结果之前处理的项目数。

在一个响应中,DynamoDB returns 所有在Limit值范围内的匹配结果。例如,如果您发出限制值为 6 且没有筛选表达式的查询或扫描请求,DynamoDB return 将匹配 table 中指定关键条件的前六个项目请求(或者在没有过滤器的情况下只是前六个项目)。如果您还提供 FilterExpression 值,DynamoDB 将 return 前六个中也符合筛选要求的项目(结果数 returned 将小于或等于 6)。

但是我找不到设置响应限制的方法。我找到了 QueryResult:

的方法 SetCount
QueryResult result2 = dynamodb.query(request);
    result2.setCount(5);

它说:那么 Count 是应用过滤器后 returned 的项目数

但我觉得这不是我想要的。因为 DynamoDb 在调用 setCount 之前仍然 returns 所有匹配项。谁能帮帮我?

您需要在发送给 API 的请求中应用限制,而不是在响应中。

我假设您提交给 dynamodb 对象的请求对象是一个 QuerySpec. What you will want to do is is call withMaxResultSize 以在查询是 运行 之前传入您想要应用的限制 API。

但是,正如您在问题中提到的,您需要确保了解 Limits 上 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).

这意味着如果您不使用 FilterExpression,您可能没问题。但是,如果您要过滤结果,您收到的结果可能会少于您的限制,因为从技术上讲,限制不是 return 的结果数量,而是 DynamoDB 可能 return.[=13= 的项目数量]

听起来您正在寻求一种方法,让 DynamoDB 在应用 FilterExpression 的同时将其 return 的结果数量限制为精确数量。不幸的是,目前 DynamoDB 无法做到这一点。

这是使用 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);

这是您可以使用 java 限制来自 dynamodb 的数据的方法。

ItemCollection<ScanOutcome> collection = null;

try {
    
    DynamoDB dynamoDB = DynamoDBClientUtil
           .getDynamoDBClient(accessKeyId, secretAccessKey, arnRole);          
    
    Table table = dynamoDB.getTable(tableName);
    ScanSpec specification = new ScanSpec();        
    specification.setMaxResultSize(10);             
    collection = table.scan(specification);

} catch (Exception ex) {
    log.error(ex.getMessage());
}

return collection;