I'm a big fan of cloud computing in general and of Amazon Web Services in particular. I honestly believe that in a few years big providers will host all, or almost all, computing and storage resources. When this is the case, we won't have to worry too much anymore about downtime, backups and system administrators. DynamoDB is one of the steps towards this future.
DynamoDB is a NoSQL database accessible through RESTful JSON API. Its design is relatively simple. There are tables, which basically are collections of data structures, or in AWS terminology, "items."
Every item has a mandatory "hash," an optional "range" and a number of other optional attributes. For instance, take the example table
Let's say we want to add a new item to the table above. RESTful call
putItem looks like (in essence):
This is what the Amazon server needs to know in order to create a new item in the table. This is how you're supposed to make this call through the AWS Java SDK:
The above script works fine, but there is one major drawback—it is not object oriented. It is a perfect example of an imperative procedural programming.
My code is not only shorter, but it also employs encapsulation and separates responsibilities of classes.
Table class (actually it is an interface internally implemented by a class) encapsulates information about the table, while
Item encapsulates item details.
We can pass an
item as an argument to another method and all DynamoDB related implementation details will be hidden from it. For example, somewhere later in the code:
In this script, we don't know anything about DynamoDB or how to deal with its RESTful API. We interact solely with an instance of
By the way, all public entities in jcabi-dynamo are Java interfaces. Thanks to that, you can test and mock the library completely (but I would recommend to use DynamoDB Local and create integration tests).
Let's consider a more complex example, which would take a page of code if we were to use a bare AWS SDK. Let's say that we want to remove all workers from our table who work as architects: