简体   繁体   中英

How to scan all items in dynamo db?

I have the following method to scan all items in dynamo db table

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 ?

Well I am not sure it this one is better but this is how I am doing it (reading through dynamodb documentation):

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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