DynamoDB Client vs DynamoDBMapper
Recently I came across a situation to access Dynamo DB (DDB) for one of my requirements. In this blog I will share them with you, let’s see the two usual ways of accessing DDB programmatically. In the following blog, i have discussed Dynamodb mapper with more examples.
DDBClient
DDBClient is the regular way to access DDB. Let’s take an example, where we have some customer information stored in UserDataTable. The POJO that goes in and out of the table will look like below.
@Data //lombok annotation.
public class UserData {
private String id; //hash key
private String discount; //range key
private String status; //table attribute
}
Now for saving this data to ddb, we need a mapper that will convert the UserData into a map having key as the fields and values as the AttributeValue. And we have to do the same thing for converting back the data from ddb item to the POJO.
Below is the code example with comments explaining the steps involved,
//1. Creating DDBClient
AmazonDynamoDB ddbClient = AmazonDynamoDBClientBuilder.standard()
.withCredentials(new AWSCredentialsProvider(KEY))
.withRegion("us-west-2")
.build();
//2. Converts the userData into Map (assuming userData object is passed)
Map<String, AttributeValue> userDataMap = new HashMap<>();
userDataMap.put(UserDataTable.TableField.ID, userData.getId());
userDataMap.put(UserDataTable.TableField.DISCOUNT, userData.getDiscount())…