繁体   English   中英

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

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

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

代码片段:

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

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

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

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

在那个例子中它是:

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

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

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

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

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

有点晚,但是

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

和样本类

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

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

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

注意: ITERATION_ONLY模式不支持loadAllResults方法。

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

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

这将起作用,它将迭代所有项目,然后返回一个列表。

暂无
暂无

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

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