2
votes

I have created a table in DynamoDB from aws console. while writing code for it, I am not able to find how to declare a sort key which is associated to a partition key


    Table: Employee
    ----------------------------------------------
    deptId            | Partition key
    empId             | sort key for deptId
    creationDateTime  | global secondary index
    ----------------------------------------------

Class Definition:


    @DynamoDBTable(tableName = "Employee")
    public class Employee {

      // I have created deptId as partition key
      @DynamoDBHashKey(attributeName = "deptId")
      private String dept;

      // I have created empId as sort key for deptId (partition key)
      private String empId;

      // I want to run search query on empId as well
      @DynamoDBIndexHashKey(attributeName = "empId", globalSecondaryIndexName = "empId-index")

      @DynamoDBIndexHashKey(attributeName = "creationDateTime", globalSecondaryIndexName = "creationDateTime-index")
      private String creationDateTime;
    }

My question is what annotation should I use and how before the declaration of empId, that will declare that empId is sort key for deptId (which is a partition key)

I have searched around and found that @DynamoDBIndexRangeKey should be used for that but that annotation does link a sort key with partition key

please help me with that, thanks in advance

2

2 Answers

8
votes

The annotation to define the sort key is DynamoDBRangeKey

@DynamoDBRangeKey
private String empId;

Range Key Annotation

0
votes

If the table has a composite primary key (partition key and sort key), you must specify both the DynamoDBHashKey and DynamoDBRangeKey attributes in your class mapping.

[DynamoDBTable("Reply")]
public class Reply {
   [DynamoDBHashKey] //PrimaryKey
   public int ThreadId { get; set; }
   [DynamoDBRangeKey] //SortKey
   public string Replenishment { get; set; }
   // Additional properties go here.
}