問題描述
在我的 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 返回限制值范圍內的所有匹配結果.例如,如果您發出限制值為 6 且沒有過濾器表達式的 Query 或 Scan 請求,則 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);
上面說:那么Count就是應用過濾器后返回的項目數
但我認為這不是我想要的.因為 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 對象的請求對象是 QuerySpec.您要做的是調用 withMaxResultSize 在針對 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 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 返回的匹配項的限制?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!