Usually, programmatic interaction with an AWS service is done using the SDK. But for DynamoDB there is another official option provided by AWS team: a library that helps you mapping an object class to a table. In a way, it’s what a object-relational mapping framework (like Hibernate or Carbonado) does for a relational database.
We created a small project to demo how to use DynamoDB mapper for several basic operations. Also, the official documentation provided by AWS is good, showing relevant examples.
The package we created shows how to work with DynamoDB. It is available here and contains examples for all operations and also for global secondary indexes. Also, the table schema is described in the object model class: long story short, the table contains a primary key and 3 global secondary indexes. For the sake of the exemplification, 2 GSIs shares a sort key.
Our conclusions and suggestions could be found below:
– We don’t encourage you to use @DynamoDBTable annotation, but instead use a DynamoDBMapperConfig. In that way, you can set the table name from a configuration file and not as a hardcoded value in your code.
AmazonDynamoDB dynamoDB = new ...; DynamoDBMapperConfig.Builder builder = DynamoDBMapperConfig.builder(); builder.setTableNameOverride(DynamoDBMapperConfig.TableNameOverride.withTableNameReplacement("borrows")); DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB, builder.build());
– This library is good especially if you have simple put and get operations. The other two (update and delete), requires the object to be loaded, which could be expensive, as you can see in our examples
– Interaction with GSIs is very similar to the SDK one, excluding the fact you have to explicitly fetch field values.
Overall, we consider this a good library, but our advice is to not hurry to add it into your code if you don’t see an immediate benefit. Keep in mind one aspect: if tomorrow DynamoDB will launch a new feature, don’t expect to be supported in that library, but for sure it will be available in the SDK. So, weight it well before taking a decision!