DynamoDBScanExpression withLimit 返回的记录多于 Limit [英] DynamoDBScanExpression withLimit returns more records than Limit

查看:12
本文介绍了DynamoDBScanExpression withLimit 返回的记录多于 Limit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须列出 DynamoDB 表中的所有记录,而不需要任何过滤器表达式.我想限制记录的数量,因此使用带有 setLimit 的 DynamoDBScanExpression.

Have to list all the records from a DynamoDB table, without any filter expression. I want to limit the number of records hence using DynamoDBScanExpression with setLimit.

DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
....
// Set ExclusiveStartKey
....
scanExpression.setLimit(10);

但是,扫描操作总是返回 10 个以上的结果!!!!这是预期的行为吗?如果是,如何?

However, the scan operation returns more than 10 results always !!!! Is this the expected behaviour and if so how?

推荐答案

Python 答案

无法为 scan() 操作设置 limit,但是可以使用 query 来设置.

It is not possible to set a limit for scan() operations, however, it is possible to do so with a query.

query 搜索items,即数据库中的行.它从列表的顶部或底部开始,并根据设置的条件查找项目.您必须有一个分区和一个排序键才能执行此操作.

A query searches through items, the rows in the database. It starts at the top or bottom of the list and finds items based on set criteria. You must have a partion and a sort key to do this.

另一方面,扫描搜索的是 ENTIRE 数据库,而不是按项目搜索,因此,它是 NOT 有序的.

A scan on the other hand searches through the ENTIRE database and not by items, and, as a result, is NOT ordered.

由于查询基于项目,扫描基于ENTIRE数据库,因此只有查询可以支持限制.

Since queries are based on items and scan is based on the ENTIRE database, only queries can support limits.

要回答 OP 的问题,基本上它不起作用,因为您使用的是 scan 而不是 query.

To answer OP's question, essentially it doesn't work because you're using scan not query.

这是一个如何使用 CLIENT 语法的示例.(更高级的语法版本.抱歉,我没有使用 resource 的更简单示例.你可以谷歌一下.)

Here is an example of how to use it using CLIENT syntax. (More advanced syntax version. Sorry I don't have a simpler example that uses resource. you can google that.)

    def retrieve_latest_item(self):
        result = self.dynamodb_client.query(
        TableName="cleaning_company_employees",
        KeyConditionExpression= "works_night_shift = :value",
        ExpressionAttributeValues={':value': {"BOOL":"True"}},
        ScanIndexForward = False,
        Limit = 3
        )
    return result

这里是 DynamoDB 模块文档

这篇关于DynamoDBScanExpression withLimit 返回的记录多于 Limit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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