简体   繁体   English

如何扫描Dynamo数据库中的所有项目?

[英]How to scan all items in dynamo db?

I have the following method to scan all items in dynamo db table 我有以下方法来扫描dynamo db表中的所有项目

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

Is it the right way or I can improve it? 是正确的方法还是我可以改善它? What is the best practice to use LastEvaluatedKey ? 使用LastEvaluatedKey的最佳实践是什么?

Well I am not sure it this one is better but this is how I am doing it (reading through dynamodb documentation): 好吧,我不确定这是不是更好,但这就是我的做法(阅读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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM