Enhanced DynamoDB Client — Java Abstraction Code

DynamoDb has introduced an enhanced client in 2020 that comes bundled with AWS SDK 2.0. This client is now the suggested way forward to execute database operations on DynamoDB using application classes.

In my recent project, we have had few scenarios to build against —

  1. Execute Insert + Update as set in a transaction. The transactions should fail for the inserts against which the primary key (Partition & Sort) already exists. Also, updates should fail if the record is updated by another process.

Duplicate inserts can be controlled through condition expression in the statements. This is a repeated pattern. There is a need to abstract this out such that not all developers need to delve deeper into dynamo code.

I have created a Helper code, which takes a java object and seamlessly executes the transaction onto DynamoDb. The helper code is available in this github repo. The readme on the repo has greater detail.

There are 3 custom annotations in addition to Dynamodb enhance client annotations that you have to apply on Java POJO.

  1. @Table(name = “ACCOUNT”) — This is where you specify the DynamoDB table in which you post objects.

  2. @PartitionKey — This is a field level annotation. You mark out the partition key relevant to the item

  3. @SortKey — This is a field level annotation. You mark out the sort key of the item

Java
 




xxxxxxxxxx
1
36


1
@Data
2
@ToString
3
@Table(name = "ACCOUNT")
4
@DynamoDbBean
5
public class FakeStudent {
6
 
          
7
    // Department
8
    @PartitionKey
9
    private String PARTITION_KEY;
10
 
          
11
    // roll number
12
    @SortKey
13
    private String SORT_KEY;
14
 
          
15
    private Integer fees;
16
    private String ID;
17
    private String STUDENT_NAME;
18
    private FakePicture img;
19
    private Integer version;
20
 
          
21
 
          
22
    @DynamoDbVersionAttribute
23
    public Integer getVersion() {
24
        return version;
25
    }
26
 
          
27
    @DynamoDbPartitionKey
28
    public String getPARTITION_KEY() {
29
        return PARTITION_KEY;
30
    }
31
 
          
32
    @DynamoDbSortKey
33
    public String getSORT_KEY() {
34
        return SORT_KEY;
35
    }
36
}
37
 
          



Once POJO is annotated, the developer needs to create a transaction packet and just execute the transaction.

Java
 




xxxxxxxxxx
1


 
1
 EHelper.TxnPacket packet = EHelper.TxnPacket.builder()
2
   .update(item1, FakeStudent.class, null )
3
   .insert(item2, FakeStudent.class)
4
   .build();
5
 
          
6
EHelper.executeTransactionWrite(packet);



The optimistic handling of updates through versioning of items, using @DynamoDbVersionAttribute is already provided by Enhanced client.

2. Query Dynamo with a variety of sortkey access patterns. The helper class includes methods, which allow developers to use java objects abstraction to query dynamo tables.

The above three annotations of @Table, @ParitionKey, and @SortKey need to be applied. Example 

 *  Fetch a single item given a partition key and sort key

Java
 




x





1
FakeStudent s1 = new FakeStudent();
2
// department
3
s1.setPARTITION_KEY("Engineering");
4
// roll number;
5
s1.setSORT_KEY("120");
6
 
          
7
FakeStudent item1 = EHelper.getItem(s1);



* Fetch a list of items for a given partition key and greater than the provided sort key

Java
 




x






1
FakeStudent s1 = new FakeStudent();
2
// department
3
s1.setPARTITION_KEY("Engineering");
4
// roll number;
5
s1.setSORT_KEY("120");
6
 
          
7
final List<FakeStudent> item1 = EHelper.querySortKeyGreaterThanOrEqualTo(s1,null,true);



The helper also supports condition expressions. The value attribute maps are automatically created through a mix of Java Reflection and Schema support libraries provided by Enhanced Client.

This helper will evolve over time. Currently, @PartitionKey support is limited to Integer and String classes. I hope this library will save time and effort and increase speed on the project.

Happy Coding!

 

 

 

 

Top