我需要获得dynamodb表中行的确切计数。我对java很陌生,所以这有点让人费解。我的理解是你不能使用描述表,因为它每6个小时更新一次。
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
Table table = dynamoDB.getTable("<table name>");
ScanResult result = new ScanResult();//不确定这最后一行。有人知道解决办法吗?System.out.println(result.withCount(1));
发布于 2017-03-22 18:01:06
这是获取项目计数的代码。
请注意,在扫描表中的所有项目以获取计数时,可能会花费。希望您不会在具有数百万项的表上运行此代码。
public Long getCountOfItems(String tableName) {
Long numberOfItems = 0L;
try {
ScanResult result = null;
do {
ScanRequest scanRequest = new ScanRequest().withTableName(tableName);
if (result != null) {
scanRequest.setExclusiveStartKey(result.getLastEvaluatedKey());
}
result = dynamoDBClient.scan(scanRequest);
numberOfItems = numberOfItems + result.getItems().size();
} while (result.getLastEvaluatedKey() != null);
} catch (Exception db) {
throw new RuntimeException("Record count couldn't be calculated ...", db);
}
System.out.println(numberOfItems);
return numberOfItems;
}发布于 2018-10-02 08:59:05
由于扫描结果有1MB限制,有时行数多于返回行数。所以,只要做一个正常的扫描,并检查你的结果,如果它包含一个LastEvaluatedKey。如果是这种情况,则继续扫描,但使用setExclusiveStartKey从最后一个记录的位置开始扫描。
ScanResult scanResult = client.scan(scanRequest);
int total = 0;
while(true)
{
total += scanResult.getScannedCount();
Map<String, AttributeValue> pages = scanResult.getLastEvaluatedKey();
if (pages == null)
break;
scanRequest.setExclusiveStartKey(pages);
scanResult = client.scan(scanRequest);
}https://stackoverflow.com/questions/42956699
复制相似问题