Member-only story
Abstracting DynamoDB Access Layer in Java
AWS SDK offers different ways to access your DynamoDB Amazon. I asked ChatGPT, Google Gemini, and Bing Generative AI to see what is the suggested default way of creating a data access layer.
The result was a bit disappointing since they all chose accessing DynamoDB using direct DynamoDB client every time (prompted 5 times on each one of them). While this serves the purpose, but it is a bit outdated and more complex.
AWS SDK provides an alternative way of interacting with the data layer with higher level of abstraction in DynamoDBMapper. The mapper is much easier to implement if you are using the data as a POJO (which most of the cases, you would probably do).
Let’s compare getting a record from a table.
public Item getItem(String tableName, String primaryKey) {
AmazonDynamoDB dynamoDB = AmazonDynamoDBClientBuilder.standard().build();
DynamoDB db = new DynamoDB(dynamoDB);
Table table = db.getTable(tableName);
return table.getItem(new GetItemSpec().withPrimaryKey("primaryKey", primaryKey));
}
In the example above, we get the results in Item object, which we need to extract the required data ourselves and translate them into POJO classes.
Here is a dirty implementation of doing that with the infamous Person class.