简体   繁体   中英

DynamoDB order in descending based on sort key

Currently, I'm using Spring boot with DynamoDB and using DynamoDBMapper for all DB operation. Right now it's sorting in ascending order based on sort key(field name id ). How can I sort in descending order based on sort key value?

Current code :

@Override
public PageImpl<Order> getCustomerOrders(Pageable pageable) {

    List<Order> orders = new ArrayList<>();


    Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();


    DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
    List<String> exp = new ArrayList<>();


    // filters

    if (!exp.isEmpty())
        scanExpression
                .withFilterExpression(String.join(" AND ", exp)).withExpressionAttributeValues(eav);


    PaginatedScanList<Order> scan = mapper.scan(Order.class, scanExpression);




    int start = (int) pageable.getOffset();
    int size = scan.size();
    int end = (start + pageable.getPageSize()) > size ? size : (start + pageable.getPageSize());

    if (start < size)
        for (Order o : scan.subList(start, end)) {
            orders.add(o);
        }

    return new PageImpl<Order>(orders, pageable, size);
}

After some googling, I found that I need to set ScanIndexForward to be true but still I'm confused that how can I achieve it with DynamoDBMApper?

Or should I need to use any low-level class like DynamoDB or AmazonDynamoDb?

Finally, I found the solution myself, Thanks to @ydrall for the hint. As per his comment, ScanIndexForward can only be used with query option so I just updated with query . I had created DynamoDBQueryExpression object and updated the property using setScanIndexForward method ( false ).

Full java code :

@Override
public PageImpl<Order> getAllProgramOrder(Pageable pageable, OrderFilter filter) {
    List<Order> orders = new ArrayList<>();


    DynamoDBQueryExpression<Order> queryExpression = new DynamoDBQueryExpression<Order>();

    queryExpression.setScanIndexForward(false);


    Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
    eav.put(":val1", new AttributeValue().withS(authenticationFacade.getProgramId()));

    queryExpression.withKeyConditionExpression("hashKey = :val1 ")
            .withExpressionAttributeValues(eav);

    PaginatedQueryList<Order> scan = mapper.query(Order.class, queryExpression);


    int start = (int) pageable.getOffset();
    int size = scan.size();
    int end = (start + pageable.getPageSize()) > size ? size : (start + pageable.getPageSize());

    if (start < size)
        for (Order o : scan.subList(start, end)) {
            orders.add(o);
        }

    return new PageImpl<Order>(orders, pageable, size);
}

Ref : How can I Scan an index in reverse in DynamoDB?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM