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.
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:
In your build.gradle file, you are going to add the next dependencies as shown in the example below
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.
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.
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.
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:
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:
public void update(Document memo) { Document document = dbTable.updateItem(memo, new UpdateItemOperationConfig().withReturnValues(ReturnValue.ALL_NEW)); }
public void delete(Document memo) { dbTable.deleteItem( memo.get("userId").asPrimitive(), memo.get("noteId").asPrimitive()); }
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.
Imagine launching your product with just the core features, getting honest user feedback, and then…
When a tight deadline is non-negotiable, every second counts! Here’s how we developed and launched…
You may have considered hiring a nearshore software development company or services, but you still have doubts…
End-to-end project management goes as far back as you can remember. Every project in history, even…
AWS DevOps has recently become a trending topic in IT circles as it offers companies…
When understanding AI vs Machine Learning, it’s essential to grasp how these innovations shape the…