python - 如何扫描dynamodb中的所有项目?

标签 python algorithm python-3.x amazon-dynamodb

我有以下方法扫描 dynamo 数据库表中的所有项目

async def scan_all(self, parse_item: Callable[[dict], dict], **kwargs) -> List[dict]:
    response = await self._db.scan(**kwargs)
    response_items = response.get('Items', [])
    items = list(parse_item(item) for item in response_items)
    last_evaluated_key = response.get('LastEvaluatedKey', None)
    while last_evaluated_key is not None:
        response = await self._db.scan(ExclusiveStartKey=last_evaluated_key, **kwargs)
        response_items = response.get('Items', [])
        items.extend(parse_item(item) for item in response_items)
        last_evaluated_key = response.get('LastEvaluatedKey', None)
    return items

这是正确的方法还是我可以改进它?使用 LastEvaluatedKey 的最佳做法是什么?

最佳答案

嗯,我不确定这个更好,但这就是我的做法(阅读 dynamodb 文档):

Map<String, AttributeValue> lastKeyEvaluated = null;

    do {
        ScanRequest scanRequest = new ScanRequest().withTableName(TABLE_NAME)
                .withExclusiveStartKey(lastKeyEvaluated);
        ScanResult result = client.scan(scanRequest);
        for (Map<String, AttributeValue> item : result.getItems()) {

        }
    }while(lastKeyEvaluated != null);

关于python - 如何扫描dynamodb中的所有项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46566644/

相关文章:

Python 浮点格式

algorithm - 确定日期是否在 DST 中的高效算法

python-3.x - 尝试使用 os.rename 重命名文件会引发 FileNotFoundError : [Errno 2]

python - 关于pandas中In-place内存操作的问题(1/2)

python - Cloud Function触发数据流时如何在数据流中传递requirements.txt参数?

python - 不同 Python dict() 中键的顺序

python - 在 wx 中绘制抗锯齿线?

c++ - 我如何记住这种递归关系?

algorithm - 如果一个 NP 在多项式时间内求解,可满足性能否在多项式时间内求解

python - 找不到使用函数从 mysql 打印数据的任何想法