我使用dynamodbmapper在dynamodb表中实现了扫描操作,但我没有得到所有结果。每当我运行程序时,扫描都会返回不同数量的项目。
代码段:
DyanmoDBScanExpression scanExpression = new DynamoDBScanExpression();
List<Books> scanResult = mapper.scan(Books.class, scanExpression);
我调查了一下,发现了物品扫描返回的限制。但是我找不到使用mapper从表中获取所有项目的方法!有没有办法让我可以循环遍历表格中的所有项目。我在JVM中设置了足够的堆内存,因此不存在内存问题。
答案 0 :(得分:1)
您需要迭代,直到不再返回LastEvaluatedKey。检查SDK中的一个官方示例中的完成方式:
答案 1 :(得分:1)
在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();
答案 2 :(得分:0)
扫描应该返回所有项目 问题是返回的集合延迟加载。 你需要遍历列表,当它消耗所有被提取的项目时,将在幕后进行额外的调用以引入更多项目(直到所有内容都被引入)。
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaQueryScanORMModelExample.html
在那个例子中它是:
// Work with external files.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "std_lib_facilities.h"
#include "wfile.h"
int main()
{
Point p;
vector<Point> original_points;
vector<Point> processed_points;
cout << "Please enter 7 pairs of numbers: " << endl;
for (int i = 0; i <= 6; ++i)
{
cout << "#" << i + 1 << " pair: ";
cin >> p;
original_points.push_back(p);
}
p.print_all(original_points);
cout << original_points << endl;
p.import_all(processed_points);
cout << processed_points << endl;
keep_window_open();
return 0;
}
答案 3 :(得分:0)
有点晚了,但是
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; }
}
答案 4 :(得分:0)
默认情况下,
DynamoDBMapper#scan
方法返回“延迟加载” 集合。它 最初只返回一页结果,然后提供服务 如有需要,请致电下一页。要获取所有匹配项, 遍历分页的结果集合。
但是,PaginatedScanList自带了 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());