Accessing DynamoDB from Spring Java application - Walkthrough tutorial
Target audience for this tutorial
People familiar with Spring and databases, and willing to use Amazon DynamoDB as datastore.Technologies used
- Spring Boot
- Gradle
- Jetty server
Amazon Tools
- Amazon DynamoDB
- Amazon AWS SDK
- Amazon IAM
Steps
1. Creating DynamoDB table
At this point it is assumed that you have a working AWS account. Go to DynamoDB console from you AWS dashboard. In my case the direct URL was https://us-west-2.console.aws.amazon.com/dynamodb/home?region=us-west-2
You may need to subscribe to the service if you are there for the first time. Go ahead and create a table. You won't be charged until you create a table from the console. At this time create a basic table with an index. You can later edit it to add additional fields.
If the table is 'ACTIVE', browse it and add a new test item.
Now you have enough data to be accessed from you local instance to test.
2. Creating IAM User
For you to access dynamoDB from your spring application, you will need a secret id and secret key. You can create one from IAM console.
- Go back to the main AWS dashboard (https://us-west-2.console.aws.amazon.com/console/home?region=us-west-2)
- Navigate to IAM console.
- In the IAM console, in the left hand navigation panel, click on 'Users'
- Create a new id and secret key, for user, let's say user123. Copy the id and secret key in a separate file using a text editor on your computer.
- After the user is created, click on the user123, and go to user settings page.
- Scroll down and click on 'Attach policy' under Permissions.
- From the list, select 'AmazonDynamoDBFullAccess' and click Attach policy button.
At this point you have your user created with required access to your DynamoDB service. Time to move on to your application part.
3. Setup gradle dependencies
Create a 'libs' folder in your package root directory (where build.gradle is there), and copy below aws jars in it:
- 'aws-java-sdk-1.10.8.jar'
- 'httpclient-4.3.6.jar'
- 'httpcore-4.3.3.jar'
- 'joda-time-2.8.1.jar'
Mention above jars in your build file as well,
repositories { .... mavenCentral()
flatDir {
dirs 'libs' }
...
}
dependencies { ... compile name: 'aws-java-sdk-1.10.8'
compile name: 'httpclient-4.3.6'
compile name: 'httpcore-4.3.3'
compile name: 'joda-time-2.8.1'
...
}
You may not require some of these, like joda-time-2.8.1 if you are already using latest version of it, but in my case these were required.
Where to find these jars? >>> In the AWS Java SDK. If you haven't downloaded aws-java-sdk yet, do it and extract the files out of zip. You will find these jars in 'lib' and 'third-party' folders.
5. Setup credentials
You will have to write your AWS credentials you created above in a file with path ~/.aws/credentials. Here is the content of file I created:
[default]
aws_access_key_id=AKKJH890NH8KH1HJ0JA6KL7
aws_secret_access_key=8Kd9K9aOP7lj4KOWKkap383jD0923jKK
Ofcourse the actual id and key are changed :)
This file will be used to obtain secure access to your AWS resources, in this case, DynamoDB.
4. Write the code
Now that all dependencies are in-place, it's time to write the code and test above.
Create a new file 'DynamoDBAccessor', and call the below init() method from constructor:
private static void init() throws AmazonClientException {
/* * The ProfileCredentialsProvider will return your [default] credential
* profile by reading from the credentials file located at
* (~/.aws/credentials).
*/
AWSCredentials credentials = null;
try {
credentials = new ProfileCredentialsProvider().getCredentials();
} catch (Exception e) {
throw new AmazonClientException("Cannot load the credentials from the credential profiles file. ", e);
}
dynamoDB = new AmazonDynamoDBClient(credentials);
Region usWest2 = Region.getRegion(Regions.US_WEST_2);
dynamoDB.setRegion(usWest2);
}
Now write a test method to test if you are able to connect.
private String sampleScan() {
String tableName = "Users";
ScanRequest scanRequest = new ScanRequest(tableName);
ScanResult scanResult = dynamoDB.scan(scanRequest);
return "Result: " + scanResult;}
Run the application calling the sampleScan() method. You should get result similar to this:
{Result: {Items: [{user_id={S: 1234,}, Name={S: ABCD,}}],Count: 1,ScannedCount: 1,}"}
That's it. Have fun using the highly scalable DynamoDB. If you have any questions or facing any problem, feel free to ask in comments.
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete