简体   繁体   中英

Retrieve all items ids from dynamodb java using DynamoDbMapper

My goal is to retrieve all item's ids. For that, I'm gonna use Java and DynamoDBMapper. The way to do it is to use scan . Right now, my code looks like this:

DynamoDBScanExpression paginatedScanListExpression = new DynamoDBScanExpression()
                .withLimit(10000)
                .withProjectionExpression("id");

var paginatedList = mapper.scanPage(MyObject.class, paginatedScanListExpression);

The problem is that I get only 600 ids per request. I'm aware of a DynamoDB 1MB per query limit, but still, it seems that 600 ids are too little for one request. Does anyone know how I can fetch all the ids more efficiently?

You can try with ScanSpec with ProjectionExpression which specifies the attributes you want in the scan result.

ScanSpec scanSpec = new ScanSpec().withProjectionExpression("id");

    try {
        ItemCollection<ScanOutcome> items = table.scan(scanSpec);

        Iterator<Item> iter = items.iterator();
        while (iter.hasNext()) {
            Item item = iter.next();
            System.out.println(item.toString());
        }

    }
    catch (Exception e) {
        System.err.println("Unable to scan the table:");
        System.err.println(e.getMessage());
    }

Reference - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Java.04.html

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