1

Data-REST and dynamoDB and trying to sort my GSI object as follows, is it possible to sort GSI Hashkey in dynamoDB, Domain class

@DynamoDBTable(tableName = "test")
public class Test implements Serializable{

    private static final long serialVersionUID = 1L;

    private String id;
    private String name;
    private String desc;

    @DynamoDBHashKey(attributeName="id")
    @DynamoDBAutoGeneratedKey
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @DynamoDBIndexHashKey(attributeName="name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @DynamoDBAttribute(attributeName="desc")
    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

}

public interface TestRepository extends PagingAndSortingRepository{

    @EnableScan
    @EnableScanCount
    public Page<Test> findByName(@Param("name") String name, Pageable pageable);

    public Page<Test> findByNameOrderByNameAsc(@Param("name") String name, Pageable pageable);

}

Is it possible to use orderBy on secondary Index because am using a hashkey of autogenerated so cant combine with hashkey attribute with Range key to fetch all possible results. I need to fetch names satisfying the search and orderBy name(GSI). Or else DynamoDB supports only orderBy through hashkey and range key as in Demo?

When I do this am getting an exception as "Cant use orderBy on scan query".

UPDATED: Am using Spring Data DynamoDB, I have created a GSI hash and key index in DynamoDB in the name "description-name-index" and I like to sort its data using range key by findBy of hash key. Here is my test code, Domain Class

@DynamoDBTable(tableName = "test")
public class Test implements Serializable{

    private static final long serialVersionUID = 1L;

    private String id;
    private String description;
    private String name;

    @DynamoDBIndexHashKey(attributeName="description")
    public String getDescription() {
        return tesId;
    }

    public void setDescription(String description) {
        this.setDescription(description);;
    }

    @DynamoDBIndexRangeKey(attributeName="name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.setName(name);
    }

    @DynamoDBHashKey(attributeName="id")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}

and tries to fetch and sort data using findByDescriptionOrderByNameDesc(@Param("decription") String description, Pageable pageable) and also I tried using @Id and placed index hash and range key in a class but all in vain. Am getting 500 Internal Error but not throwing any message too. Is there any way to use GSI hash and range pair and sort them.

mkobit
  • 43,979
  • 12
  • 156
  • 150
jAddict
  • 395
  • 3
  • 6
  • 18

1 Answers1

1

The error message you get makes sense to me because scan in DynamoDB will return you all the items in random order (i.e. you can't specify order for scan operation).

You want to get all the rows sorted by the name. One naive approach would be to create a GSI with a single dummy hash-key and the range key would be name. All of the rows in that GSI will have the same hashkey. This way GSI can sort all of your rows by name.

The above solution would work but it has a hostspot issue...

Erben Mo
  • 3,528
  • 3
  • 19
  • 32
  • Here having a GSI key to sort or sorting the scanned output in java Iterator is good option. Which one I should go and which one will be effective too?? – jAddict Jun 16 '14 at 16:13
  • I would suggest sorting the scanned output as it is more straightforward and less error-prone – Erben Mo Jun 16 '14 at 21:27