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 —
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.
@Table(name = “ACCOUNT”) — This is where you specify the DynamoDB table in which you post objects.
@PartitionKey — This is a field level annotation. You mark out the partition key relevant to the item
@SortKey — This is a field level annotation. You mark out the sort key of the item
xxxxxxxxxx
name = "ACCOUNT") (
public class FakeStudent {
// Department
private String PARTITION_KEY;
// roll number
private String SORT_KEY;
private Integer fees;
private String ID;
private String STUDENT_NAME;
private FakePicture img;
private Integer version;
public Integer getVersion() {
return version;
}
public String getPARTITION_KEY() {
return PARTITION_KEY;
}
public String getSORT_KEY() {
return SORT_KEY;
}
}
Once POJO is annotated, the developer needs to create a transaction packet and just execute the transaction.
xxxxxxxxxx
EHelper.TxnPacket packet = EHelper.TxnPacket.builder()
.update(item1, FakeStudent.class, null )
.insert(item2, FakeStudent.class)
.build();
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
x
FakeStudent s1 = new FakeStudent();
// department
s1.setPARTITION_KEY("Engineering");
// roll number;
s1.setSORT_KEY("120");
FakeStudent item1 = EHelper.getItem(s1);
* Fetch a list of items for a given partition key and greater than the provided sort key
x
FakeStudent s1 = new FakeStudent();
// department
s1.setPARTITION_KEY("Engineering");
// roll number;
s1.setSORT_KEY("120");
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!