如何使用 AWS Java SDK 按降序从 DynamoDb 获取项目?
How to get items from DynamoDb in Descending order with AWS Java SDK?
我在 AWS DynamoDB 中有一个名为 accountantHistoric 的 table。它有一个主分区键 (id) 和一个主排序键 (tenant_id)。我还创建了另一个索引,称为 "ExpirationDate"。该索引的排序键是 "expirationDate"(String) 属性。我想要的是:获取基于 storeId 的 itens(此字段不是键)和基于 "expirationDate"(从最新到最旧)降序排列的 tenant_id。我也在做分页。
我正在尝试使用下面的代码,但它没有按降序返回。
我需要更改什么?
谢谢大家!
public AccountantEmailHistoricInfo getHistoric(String storeId, int page, int countReg, int fixedPage) {
Condition conditionStoreId = new Condition().withComparisonOperator(ComparisonOperator.EQ)
.withAttributeValueList(new AttributeValue().withS(storeId));
Condition conditionTenantId = new Condition().withComparisonOperator(ComparisonOperator.EQ)
.withAttributeValueList(new AttributeValue().withS(TenantIdentifierLocalThread.getTenantIdentifier()));
final DynamoDBScanExpression scanPageExpression = new DynamoDBScanExpression()
.withFilterConditionEntry("tenant_id", conditionTenantId)
.withFilterConditionEntry("storeId", conditionStoreId)
.withIndexName("ExpirationDate")
.withLimit(countReg);
List<AccountantEmailHistoric> accountantEmailHistoricList = new ArrayList<>();
int i = 0;
do {
ScanResultPage<AccountantEmailHistoric> scanPage = mapper.scanPage(AccountantEmailHistoric.class, scanPageExpression);
scanPageExpression.setExclusiveStartKey(scanPage.getLastEvaluatedKey());
if (i == page) {
accountantEmailHistoricList.addAll(scanPage.getResults());
}
i++;
} while (scanPageExpression.getExclusiveStartKey() != null);
final DynamoDBScanExpression scan = new DynamoDBScanExpression()
.withFilterConditionEntry("tenant_id", conditionTenantId)
.withFilterConditionEntry("storeId", conditionStoreId);
AccountantEmailHistoricInfo info = new AccountantEmailHistoricInfo(accountantEmailHistoricList);
info.setPage(fixedPage);
info.setTotalRecords(mapper.scan(AccountantEmailHistoric.class, scan).size());
return info;
}
您需要query,而不是扫描。然后你可以把ScanIndexForward
设为false。
我在 AWS DynamoDB 中有一个名为 accountantHistoric 的 table。它有一个主分区键 (id) 和一个主排序键 (tenant_id)。我还创建了另一个索引,称为 "ExpirationDate"。该索引的排序键是 "expirationDate"(String) 属性。我想要的是:获取基于 storeId 的 itens(此字段不是键)和基于 "expirationDate"(从最新到最旧)降序排列的 tenant_id。我也在做分页。
我正在尝试使用下面的代码,但它没有按降序返回。
我需要更改什么?
谢谢大家!
public AccountantEmailHistoricInfo getHistoric(String storeId, int page, int countReg, int fixedPage) {
Condition conditionStoreId = new Condition().withComparisonOperator(ComparisonOperator.EQ)
.withAttributeValueList(new AttributeValue().withS(storeId));
Condition conditionTenantId = new Condition().withComparisonOperator(ComparisonOperator.EQ)
.withAttributeValueList(new AttributeValue().withS(TenantIdentifierLocalThread.getTenantIdentifier()));
final DynamoDBScanExpression scanPageExpression = new DynamoDBScanExpression()
.withFilterConditionEntry("tenant_id", conditionTenantId)
.withFilterConditionEntry("storeId", conditionStoreId)
.withIndexName("ExpirationDate")
.withLimit(countReg);
List<AccountantEmailHistoric> accountantEmailHistoricList = new ArrayList<>();
int i = 0;
do {
ScanResultPage<AccountantEmailHistoric> scanPage = mapper.scanPage(AccountantEmailHistoric.class, scanPageExpression);
scanPageExpression.setExclusiveStartKey(scanPage.getLastEvaluatedKey());
if (i == page) {
accountantEmailHistoricList.addAll(scanPage.getResults());
}
i++;
} while (scanPageExpression.getExclusiveStartKey() != null);
final DynamoDBScanExpression scan = new DynamoDBScanExpression()
.withFilterConditionEntry("tenant_id", conditionTenantId)
.withFilterConditionEntry("storeId", conditionStoreId);
AccountantEmailHistoricInfo info = new AccountantEmailHistoricInfo(accountantEmailHistoricList);
info.setPage(fixedPage);
info.setTotalRecords(mapper.scan(AccountantEmailHistoric.class, scan).size());
return info;
}
您需要query,而不是扫描。然后你可以把ScanIndexForward
设为false。