简体   繁体   English

使用 DynamoDbMapper 从 dynamodb java 检索所有项目 ID

[英]Retrieve all items ids from dynamodb java using DynamoDbMapper

My goal is to retrieve all item's ids.我的目标是检索所有项目的 ID。 For that, I'm gonna use Java and DynamoDBMapper.为此,我将使用 Java 和 DynamoDBMapper。 The way to do it is to use scan .方法是使用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.问题是我每次请求只能得到 600 个 ID。 I'm aware of a DynamoDB 1MB per query limit, but still, it seems that 600 ids are too little for one request.我知道每个查询限制 DynamoDB 1MB,但对于一个请求来说,600 个 ID 似乎还是太少了。 Does anyone know how I can fetch all the ids more efficiently?有谁知道如何更有效地获取所有 ID?

You can try with ScanSpec with ProjectionExpression which specifies the attributes you want in the scan result.您可以尝试将 ScanSpec 与 ProjectionExpression 一起使用,它在扫描结果中指定您想要的属性。

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参考 - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Java.04.html

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

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