Amazon DynamoDB local instance tutorial
Contents
Let’s take a look how neat and nice could be code for persisting in Amazon’s DynamoDB service using Spring data dynamodb project. Speaking of aws dynamodb, you can’t avoid to use local version of dynamodb during development cycle. So, we slightly touch this option in this tutorial.
tip
DynamoDb is a NoSQL highly scalable database from Amazon Web Services.
The full example code of the tutorial could be found on github.
Project setup
Easily start the blank spring project:
|
|
tip
You can do it manually on spring initializer.
Unpack archive, which get after curl executed. Now you are ready to go.
Spring data dynamodb
This is an extension of well-known project Spring data, thus it follows all main abstractions of spring data,
like CrudRepository
for example. It makes the start is super easy to those fellows, who are familiar with Spring data.
Let’s add spring data dynamodb to our project.
|
|
The next thing we need to do is to add the POJO of the object being persisted in dynamodb and its repository.
|
|
|
|
Connecting to the cloud
Now we need to configure the spring data dynamodb, to let it know how to connect to the dynamodb.
Add a new class to the project - DynamoDBConfigProd
:
|
|
This is a Spring configuration class. EnableDynamoDBRepositories
annotation should point to the repository, which we created.
Also this configuration is for using real dynamodb in the aws, so let’s mark it as prod profile.
In this configuration we are creating the bean of DynamoDB, providing aws credentials for connection.
The aws key/secret pair should be added to application.yml
file:
|
|
Also, you can notice, that we are enabled the schema auto creation option. By this option spring data dynamodb will manage all need preconditions according our model and create the schema objects if needed.
warning
entity2ddl is more for tutorials and playground goals and shouldn’t be used in real production.
Now, we need to run something. Let’s make a simple example - just put some User to the dynamodb and when check how many we have.
Add CommandLineRunner
to the main Application class:
|
|
Ok, let’s run it:
|
|
note
Probably you will need to pass D-property to the bootRun task like this:
|
|
in build.gradle file
Somewhere in the console you should see:
|
|
And every time you run the code, the more users will be in the table. Actually, all the magic already happened - application connected to the aws dynamodb, created the User table and added the item in it. But lets proof that there are some items indeed.
|
|
the response:
|
|
And what items do we have inside?
|
|
the response:
|
|
Pretty much easy, yes? That is how approximately we can use the dynamodb in the project. But during development, it is more convenient to have local instance of dynamodb, so let’s take a look how we can arrange this also.
Local Amazon dynaomdb configuration
First, we would need the separate configuration for that, so let’s add this to the root package of the project:
|
|
This configuration available for all profiles, except the prod. The main part here is the starting server by means of
ServerRunner.createServerFromCommandLineArgs()
. Looks simple, but there are a few gotcha’s, which you need to solve before
start using the local dynamodb.
First the fakekey/fakeSecret is not a garbage here, they are realy needed to local dynamodb worked. For some reason it requires AWS credentials, even though they are not needed. As you understand, there is no real fakekey and fakeSecret.
The second thing, which should be taken into account is the native libraries, on which the local dynamodb depends. This is part of internal implementation of dynamodb: it runs the sql-lite inside, but with the DynamoDB API exposed to the user. So it should be clear, that this approach can’t be used for running production servers, it is only for development lifecycle locally.
Ok, so, why we are talking so much about this internal stuff? Because sometimes it turns so, that the user should take care about the native libs - the simple way to do that is to put them in one place in project build folder and “tell” to the dynamodb where they are. So that’s how we do that:
add this task to build.gradle
|
|
and there is already set system property in our config file above:
|
|
tip
Of course, you can set the system property in the way, which is more suitable to you project settings.
Oh, almost forget, we added a few properties, which should be added to the application.yml:
|
|
Ok, now we are ready to go further: run
|
|
and see
|
|
and this time you will get 1 users each time, because the dynamodb will be created from scratch every time in memory.
Something as conclusion for the tutorial
This was a short tutorial of the way of using the dynamodb in the development. We saw a tool for making a requests to dynamodb in the aws cloud and the way to start Amazon dynamodb locally. Actually, this is a huge topic, the dynamodb tables optimization and designing of the application for the eventual consistency, for example, have been left behind the scene. But this is a good start for quick setting the project as playground with dynamodb, avoiding spending time on solving simple problems with the setup.
Author Relaximus
LastMod 2019-03-24