Introduction, Overview to Amazon Dynamo DB

Introduction, Overview to Amazon Dynamo DB

Dynamo DB is a Non-relational database that hosted as a fully managed service on AWS cloud. It is available in multiple AWS regions and provides very low latency also offers built-in security, backup and restore, and in-memory caching. This database can use for high-performance applications that need to be scaled up. In this tutorial, we see the basics of Dynamo DB and sample applications in JAVA language to do operations.

Basic Concepts

Being a non-relational database, the data that we store in this DB is not structured. To transfer data, in and out of dynamo DB we use JSON format. But Dynamo DB doesn’t store data in JSON format. So we need to send data in JSON format to DB and expect a JSON data out of it.

Tables are the data structures in Dynamo DB to store data, i.e., they are the containers to store data. The table is a schema-less one. We need to specify a primary key for the table apart from this we can have arbitrary attributes in a table. Data store as an item in a table. Each item has a unique primary key to find it.

Set up

Dynamo DB comes in 2 flavors. We can use it in the AWS console online or set it up locally. In this tutorial, we use the online version.

API support

There are SDKs in popular languages like Java, Net, JavaScript, etc. for Dynamo DB operations. We can use any of this based on our need.

AWS Java SDK

Let us see how to use the Java SDK for data manipulation in Dynamo DB. We need to add below maven dependency in our project.

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
</dependency>

We will use DynamoDBMapper class for data manipulation operations. Below code will create a DynamoDBMapper object.

AmazonDynamoDB dynamoDB = new AmazonDynamoDBClient(new DefaultAWSCredentialsProviderChain());
dynamoDB.setEndpoint(endpoint);
// here endpoint is the url of your dynamo db service
DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB);

In order to use the DynamoDB, we need to give the security credentials which we can set up using AWS CLI. DefaultAWSCredentialsProviderChain will look for the security credentials and initialize the AmazonDynamoDBClient object, which can be used to create a mapper object.

We will be using the AWS Console to create a table in Dynamo DB. Login to AWS console and go to Dynamo DB service. Give a table name and add a primary key for the table.

The Employee table will create once we submit the form.

Amazon Dynamo DB

Now we need to add a new model class for this table. This model will be used in the AWS Java SDK for data manipulation in the table.

We can use below Employee model.

@DynamoDBTable(tableName = "Employee")
public class Employee {
private String employee_id;
private String name;
@DynamoDBHashKey
@DynamoDBAutoGeneratedKey
public String getEmployee_id() {
return id;
}
@DynamoDBAttribute
public String getName() {
return name;
}
// standard setters/constructors
}

Please note that the attributes in the Employee model, are arbitrary we have not defined them while creating the table in Dynamo DB.

Now to create an entry (or item) in the Employee table. The above code will save a new Employee data in the Employee table.

Employee = new Employee ();
// Set values
dynamoDBMapper.save(partnerGroup);

To fetch details of a particular Employee, we can use the load() function.

Employee = dynamoDBMapper.load(Employee .class, "100");
To delete this Employee item from DB use
dynamoDBMapper.delete(employee );

Apart from the above load function, we can use Dynamo DB Scan and Query functions to search for data in a table. The methods query() and scan() DynamoDBMapper class can be used for this purpose. The data transfer from Dynamo DB is measured using Read Capacity Unit (RCU) and Write Capacity Unit (WCU). The main aim of any data access logic should be to keep this RCU and WCU to a minimum. The DynamoDB follows a pay per usage model, were in the usage is calculated using RCU and WCU. Those metrics should be a minimum for cost-effective usage. Both Scan and Query functions should be used judiciously to decrease the cost.

Conclusion

AWS DynamoDB is a No SQL database, hosted on the AWS cloud as a fully managed service. It offers high scalability and performance for mission-critical applications. As it is a managed service, update, back up, recovery, scaling, etc. are handled by AWS. The DynamoDB is schema-less, so we don’t need to maintain a structure for the DynamoDB tables. There are various SDKs available for data access in DynamoDB.

In this tutorial, Professional Java Developers India shows examples for accessing data using the Java SDK. As seen before, the need to minimize data transfer to reduce the cost of usage.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s