0

我在 AWS DynamoDB 中有一个名为 accountantHistoric 的表。它有一个主分区键 (id) 和一个主排序键 (tenant_id)。我还创建了另一个索引,称为“ExpirationDate”。该索引的排序键是“expirationDate”(字符串)属性。我想要的是:获取基于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;
        }
4

1 回答 1

2

您需要查询,而不是扫描。然后您可以指示ScanIndexForward设置为 false。

于 2019-07-08T22:20:10.907 回答