derjust / spring-data-dynamodb

This module deals with enhanced support for a data access layer built on AWS DynamoDB.
https://derjust.github.io/spring-data-dynamodb/
Apache License 2.0
402 stars 141 forks source link

how to use DynamoDBIndexHashKey and filter? #36

Open severe1004 opened 8 years ago

severe1004 commented 8 years ago

i want query index(idx_global_usrNo_feedRegDate) and filter attribute (feedOpenYn) error message : java.lang.UnsupportedOperationException: Sort not supported for scan expressions

this my class.,.

@DynamoDBTable(tableName = "feed_user")  
public static class FeedUser {      
    @Id
    @DynamoDBHashKey
    @DynamoDBAutoGeneratedKey
    private String id;

    @DynamoDBIndexHashKey(globalSecondaryIndexName="idx_global_usrNo_feedRegDate")      
    private int usrNo;

    @DynamoDBAttribute
    private String feedId;

    @DynamoDBAttribute
    private Date feedRegDate;

    @DynamoDBAttribute
    @DynamoDBNativeBoolean
    private boolean feedOpenYn;
}

public interface FeedUserRepository extends DynamoDBPagingAndSortingRepository<Feed.FeedUser, String>{  
    public List<Feed.FeedUser> findByUsrNo(int usrNo, Pageable pageable);   
    public List<Feed.FeedUser> findByUsrNoAndFeedOpenYn(int usrNo, boolean feedOpenYn, Pageable pageable);
}

public class DynamoDBFeedTest {
    @Test
    public void feed_test(){
        PageRequest pageRequest = new PageRequest(1, 10, new Sort(Direction.DESC, "usrNo"));
        feedUserRepository.findByUsrNo(2, pageRequest); //runnable
        feedUserRepository.findByUsrNoAndFeedOpenYn(2, true, pageRequest); //not runnable 
    }
}
derjust commented 8 years ago

You need a HASH+RANGE key to do sorting: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html

Scans can filter on attributes that are not part of an index but can not sort. That's based on the architecture of DynamoDB: While the scan is executed in parallel on the various partitions that hold the matching key (decided by the hash) it is impossible to sort all the results on-the-fly (it would only be possible with a locking and mutex and this wasn't desired by the design).

Create a HASH+RANGE key and it should work.

derjust commented 8 years ago

Please see 4509a7b79b61aa6d7f59c72d7a465dfd11f1f1a6 as an example how to achieve that (assuming I get the intention right from your code)