我在AWS DynamoDB中有一个名为accountantHistoric的表。它具有一个主分区键(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。

10-05 19:50