使用 Java 高级 API 从 DynamoDB 表中获取所有表项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31790815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:07:36  来源:igfitidea点击:

Get all the table items from DynamoDB table using Java High Level API

javaamazon-web-servicesamazon-dynamodb

提问by dushyantashu

I implemented scan operation using in dynamodb table using dynamodbmapper, but I'm not getting all the results. Scan returns different number of items, whenever I run my program.

我使用 dynamodbmapper 在 dynamodb 表中实现了扫描操作,但我没有得到所有结果。每当我运行我的程序时,扫描都会返回不同数量的项目。

Code snippet :

代码片段:

DyanmoDBScanExpression scanExpression = new DynamoDBScanExpression();
List<Books> scanResult = mapper.scan(Books.class, scanExpression);

I investigated into it, and found out about the limit of the items scan returns. But I couln't find a way to get all the items from the table using mapper! Is there a way so I can loop through all the items of the table. I have set enough heap memory in JVM so there won't be memory issues.

我调查了一下,发现了物品扫描返回的限制。但是我找不到使用映射器从表中获取所有项目的方法!有没有办法让我可以遍历表的所有项目。我在 JVM 中设置了足够的堆内存,所以不会出现内存问题。

回答by Mircea

the scan should return all the items.
the catch is that the collection returned is lazily loaded. you need to iterate through the List and when it consumes all the items that are fetched additional calls will be made behind the scenes to bring in more items (until everything is brought in).

扫描应该返回所有项目。
问题是返回的集合是延迟加载的。您需要遍历 List,当它消耗了所有获取的项目时,将在幕后进行额外调用以引入更多项目(直到所有项目都被引入)。

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaQueryScanORMModelExample.html

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaQueryScanORMModelExample.html

In that example it's:

在那个例子中它是:

List<Book> scanResult = mapper.scan(Book.class, scanExpression);

for (Book book : scanResult) {
    System.out.println(book);
}

回答by piotr

You need to iterate until LastEvaluatedKey is no longer returned. Check how is done in one of the official examples from the SDK:

您需要迭代直到不再返回 LastEvaluatedKey。在 SDK 的官方示例之一中检查是如何完成的:

https://github.com/awslabs/aws-dynamodb-examples/blob/23837f36944f4166c56988452475edee99868166/src/main/java/com/amazonaws/codesamples/lowlevel/LowLevelQuery.java#L70

https://github.com/awslabs/aws-dynamodb-examples/blob/23837f36944f4166c56988452475edee99868166/src/main/java/com/amazonaws/codesamples/lowlevel/LowLevelQuery.java#L70

回答by Jacob Joy

In java use the DynamoDBScanExpression without any filter,

在java中使用没有任何过滤器的DynamoDBScanExpression,

// Change to your Table_Name (you can load dynamically from lambda env as well)
DynamoDBMapperConfig mapperConfig = new DynamoDBMapperConfig.Builder().withTableNameOverride(DynamoDBMapperConfig.TableNameOverride.withTableNameReplacement("Table_Name")).build();

DynamoDBMapper mapper = new DynamoDBMapper(client, mapperConfig);

DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();

// Change to your model class   
List < ParticipantReport > scanResult = mapper.scan(ParticipantReport.class, scanExpression);



// Check the count and iterate the list and perform as desired.
scanResult.size();

回答by Prasanth Rajendran

By default, the DynamoDBMapper#scanmethod returns a "lazy-loaded"collection. It initially returns only one page of results, and then makes a service call for the next page if needed. To obtain all the matching items, iterate over the paginated results collection.

默认情况下,该DynamoDBMapper#scan方法返回一个“延迟加载”集合。它最初只返回一页结果,然后在需要时为下一页调用服务。要获取所有匹配项,请遍历分页结果集合。

However, PaginatedScanList comes with out of box PaginatedScanList#loadAllResultsmethod which helps to eagerly load all results for this list.

但是,PaginatedScanList 附带了开箱即用的PaginatedScanList#loadAllResults方法,有助于急切地加载此列表的所有结果。

NOTE:loadAllResultsmethod is not supported in ITERATION_ONLYmode.

注意:loadAllResultsITERATION_ONLY模式下不支持方法。

List<Books> scanResult = mapper.scan(Books.class, new DynamoDBScanExpression());
scanResult.loadAllResults();//Eagerly loads all results for this list.
//Total results loaded into the list
System.out.println(scanResult.size());

回答by gbk

A little bit late, but

有点晚,但是

import java.util.HashMap;
import java.util.Map;
import java.util.List;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;

public final class LogFetcher {

    static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
    static String tableName = "SystemLog";

    public static List<SystemLog> findLogsForDeviceWithMacID(String macID) {

        client.setRegion(Region.getRegion(Regions.EU_WEST_1));

        DynamoDBMapper mapper = new DynamoDBMapper(client);
        Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
        eav.put(":val1", new AttributeValue().withS(macID));

        DynamoDBQueryExpression<SystemLog> queryExpression = new DynamoDBQueryExpression<SystemLog>()
                .withKeyConditionExpression("parentKey = :val1")
                .withExpressionAttributeValues(eav);

         List<SystemLog> requestedLogs = mapper.query(SystemLog.class, queryExpression);

        return requestedLogs;
    }
}

And sample class

和样本类

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

@DynamoDBTable(tableName="SystemLog")
public final class SystemLog {
    public Integer pidValue;
    public String uniqueId;
    public String parentKey;

    //DynamoDB

    //Partition (hash) key
    @DynamoDBHashKey(attributeName="parentKey")
    public String getParentKey() { return parentKey; }
    public void setParentKey(String parentKey) { this.parentKey = parentKey; }

    //Range key
    @DynamoDBRangeKey(attributeName="uniqueId")
    public String getUniqueId() { return uniqueId; }
    public void setUniqueId(String uniqueId) { this.uniqueId = uniqueId;} 

    @DynamoDBAttribute(attributeName="pidValue")
    public Integer getPidValue() { return pidValue; }    
    public void setPidValue(Integer pidValue) { this.pidValue = pidValue; }
}