Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare sort key while defining table in Java for DynamoDB

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

like image 935
Paritosh Agrawal Avatar asked Jan 31 '26 10:01

Paritosh Agrawal


1 Answers

The annotation to define the sort key is DynamoDBRangeKey

@DynamoDBRangeKey
private String empId;

Range Key Annotation

like image 63
notionquest Avatar answered Feb 03 '26 09:02

notionquest