Development

How to connect Amazon DynamoDB to your android application

DynamoDB is a NoSQL database service provided by Amazon Web Services, which means it is designed to store and retrieve any amount of data you require, this can be very useful in an Android or iOS application. In this tutorial, we are going to go through all the steps of connecting Amazon DynamoDB in your android application.

Set up

Before we get started with the setup, it is important to tell you that for you to connect AWS DynamoDB, you must have an AWS account and Android studio.

After you set up your AWS account and your Android studio, you have to get the AWS Mobile SDK for Android. And how do I do that you may ask, well there are three easy ways of doing it:

1. Using gradle

In your build.gradle file, you are going to add the next dependencies as shown in the example below

  1. dependencies{
  2. compile ‘com.amazonaws:aws-android-sdk-core:2.6.+’
  3. compile ‘com.amazonaws:aws-android-sdk-s3:2.6.+’
  4. compile ‘com.amazonaws:aws-android-sdk-ddb:2.6.+’
  5. compile ‘com.amazonaws:aws-android-sdk-core:2.4.4’
  6. compile ‘com.amazonaws:aws-android-sdk-ddb:2.4.4’
  7. compile ‘com.amazonaws:aws-android-sdk-ddb-document:2.4.4’
  8. }

2. Importing Jar files

For you to get the Jar files, you have to download the SDK from http://aws.amazon.com/mobile/sdk where you’ll see the next screen.

In the android section click on the Download SDK link, it will automatically start downloading the file on your browser. Then on android studio add the downloaded file to your /libs folder and sync the project, it will be automatically updated.

3. Using maven

This SDK we are trying to download supports Apache maven, which is a software that can help manage a project’s build. Every maven project contains a pom.xml in which you can specify the services you want to be added to your project. This is helpful because it only adds the dependencies you need, not all the jar files.

Okay so now that you have the SDK installed in your project, we can proceed with the setup. The next step is pretty easy, in your AndroidManifest.xml you are going to add the permissions as shown in the example below.

  1. < uses:permission android:name=”android.permission.INTERNET” />
  2. < uses:permission android:name=”android.permission.ACCESS_NETWORK_STATE” />
  3. < uses:permission android:name=”android.permission.ACCESS_WIFI_STATE” />

The last step is that you have to get your AWS credentials.
Okay now that you have set up the AWS SDK in your project let’s continue with the connection between your project and your database stored in Amazon DynamoDB.

Creating a new table

To create a new table, we need to get into the console to give a name to the new table and select a primary key for the data that’s going to be stored in it. Next, you click continue and you have to choose the throughput capacity of the table (read capacity units and write capacity units). In the next step, you can select a limit of requests which in the case you pass it you’ll be notified via email. Lastly, click on “create table.”

Let’s get to it, now that you have all the previous set up and the table created you are ready to connect DynamoDb to your Android project. First of all, you need to import a library in your java class as shown in the example below:

  1. import com.amazonaws.auth.CognitoCachingCredentialsProvider;
  2. import com.amazonaws.mobileconnectors.dynamodbv2.document.Table;
  3. import com.amazonaws.mobileconnectors.dynamodbv2.document.UpdateItemOperationConfig;
  4. import com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document;
  5. import com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Primitive;
  6. import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
  7. import com.amazonaws.services.dynamodbv2.model.ReturnValue;

Now that you have the imports you are going to add the code shown below:

CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(), COGNITO_POOL_ID, COGNITO_REGION);

What this code does is that we are creating Amazon Cognito credentials for access permissions, next you are going to create a DynamoDbClient object using the Cognito Credentials created above as shown in the example below:

AmazonDynamoDBClient dbClient = new AmazonDynamoDBClient(credentialsProvider);

And finally you are going to create a table reference like this:

Table dbTable = Table.loadTable(dbClient, TABLE_NAME);

Now that you have the table reference you can create, read, edit and delete data from your table:

Create

  1. public void create(Document memo) {
  2. if (!memo.containsKey(“userId”)) {
  3. memo.put(“userId”, credentialsProvider.getCachedIdentityId());
  4. }
  5. if (!memo.containsKey(“noteId”)) {
  6. memo.put(“noteId”, UUID.randomUUID().toString());
  7. }
  8. if (!memo.containsKey(“creationDate”)) {
  9. memo.put(“creationDate”, System.currentTimeMillis());
  10. }
  11. dbTable.putItem(memo);
  12. }

Update

public void update(Document memo) {
Document document = dbTable.updateItem(memo, new UpdateItemOperationConfig().withReturnValues(ReturnValue.ALL_NEW));
}

Delete

public void delete(Document memo) {
dbTable.deleteItem(
memo.get("userId").asPrimitive(),
memo.get("noteId").asPrimitive());
}

Read

  1. public Document getById(String id) {
  2. return dbTable.getItem(new Primitive(credentialsProvider.getCachedIdentityId()), new Primitive(noteId));
  3. }
  1. public List <Document> getAll() {
  2. return dbTable.query(new Primitive(credentialsProvider.getCachedIdentityId())).getAllResults();
  3. }

And there you go, you have successfully connected your Android project to Amazon DynamoDb, in ClickIT we use technologies like DynamoDb and AWS to give better quality to our client’s projects and to keep them updated with today’s best technologies.

Published by
DevOps Guy

Recent Posts

How to Implement AI Data Management In Your Business

Have you ever wondered how businesses easily process enormous volumes of data, derive valuable insights,…

38 mins ago

Developing Cloud Applications: The Complete Guide for 2025

Discover the steps for developing cloud applications, from costs to cloud app deployment

1 week ago

Why Build an MVP in 2025 | Video

Imagine launching your product with just the core features, getting honest user feedback, and then…

3 weeks ago

How CDN Reduced AWS Costs For MEXICANA Airlines Web App

When a tight deadline is non-negotiable, every second counts! Here’s how we developed and launched…

3 weeks ago

Nearshore Software Development Company | Complete Guide

You may have considered hiring a nearshore software development company or services, but you still have doubts…

1 month ago

End to End Project Management: Complete Guide

End-to-end project management goes as far back as you can remember. Every project in history, even…

1 month ago