如何使用Java设置DynamoDB返回的匹配项的限制? [英] How to set limit of matching items returned by DynamoDB using Java?

查看:72
本文介绍了如何使用Java设置DynamoDB返回的匹配项的限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用中,我想从DynamoDB查询数据.将会有上千个匹配项,但我只想获取其中的前10个.我不知道如何设置此限制.我在文档中找到了以下几行:

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:

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

The DynamoDB Query and Scan APIs allow a Limit value to restrict the size of the results.

在请求中,将Limit参数设置为希望DynamoDB在返回结果之前要处理的项目数.

In a request, set the Limit parameter to the number of items that you want DynamoDB to process before returning results.

在响应中,DynamoDB返回在Limit值范围内的所有匹配结果.例如,如果您发出限制"值为6且没有过滤器表达式的查询或扫描请求,则DynamoDB会返回表中与请求中指定的关键条件匹配的前六个项目(或仅返回前六个项目).如果是不带过滤器的扫描).如果您还提供FilterExpression值,则DynamoDB将返回前六项也符合过滤器要求的项目(返回的结果数将小于或等于6).

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

But I cannot find the way to set limit for response. I found the method SetCount of QueryResult:

QueryResult result2 = dynamodb.query(request);
    result2.setCount(5);

它说:然后计数是应用过滤器后返回的项目数

但是我认为这不是我想要的.因为DynamoDb在调用setCount之前仍返回所有匹配项.有人可以帮我吗?

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?

推荐答案

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

You need to apply the limit as part of the request that you send to the API, not on the response.

我假设您要提交给dynamodb对象的请求对象是

I assume the request object you are submitting to the dynamodb object is a QuerySpec. What you will want to do is is call withMaxResultSize to pass in the limit you want applied before the query is run against the 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:

在响应中,DynamoDB返回内部所有匹配的结果. 极限值的范围.例如,如果您发出查询或扫描 限制值为6且没有过滤器表达式的请求, DynamoDB会返回表中与之匹配的前六个项目 请求中指定的关键条件(或仅前六个项目) 如果是不带过滤器的扫描).如果您还提供 FilterExpression值,DynamoDB将在第一个中返回项目 六个,它们也符合过滤器要求(结果数 返回的值将小于或等于6).

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,则可能会没事.但是,如果您对结果进行过滤,则从技术上讲,该限制不是返回的结果数,而是DynamoDB可能返回的项目数.

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.

听起来您正在寻求一种方法,让DynamoDB在应用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.

这篇关于如何使用Java设置DynamoDB返回的匹配项的限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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