Reader small image

You're reading from  DynamoDB Cookbook

Product typeBook
Published inSep 2015
Publisher
ISBN-139781784393755
Edition1st Edition
Concepts
Right arrow
Author (1)
Tanmay Deshpande
Tanmay Deshpande
author image
Tanmay Deshpande

Tanmay Deshpande is a Hadoop and big data evangelist. He currently works with Schlumberger as a Big Data Architect in Pune, India. He has interest in a wide range of technologies, such as Hadoop, Hive, Pig, NoSQL databases, Mahout, Sqoop, Java, cloud computing, and so on. He has vast experience in application development in various domains, such as oil and gas, finance, telecom, manufacturing, security, and retail. He enjoys solving machine-learning problems and spends his time reading anything that he can get his hands on. He has great interest in open source technologies and has been promoting them through his talks. Before Schlumberger, he worked with Symantec, Lumiata, and Infosys. Through his innovative thinking and dynamic leadership, he has successfully completed various projects. He regularly blogs on his website http://hadooptutorials.co.in. You can connect with him on LinkedIn at https://www.linkedin.com/in/deshpandetanmay/. He has also authored Mastering DynamoDB, published in August 2014, DynamoDB Cookbook, published in September 2015, Hadoop Real World Solutions Cookbook-Second Edition, published in March 2016, Hadoop: Data Processing and Modelling, published in August, 2016, and Hadoop Blueprints, published in September 2016, all by Packt Publishing.
Read more about Tanmay Deshpande

Right arrow

Chapter 9. Developing Web Applications using DynamoDB

In this chapter, we will cover the following topics:

  • Performing data modeling and table creations

  • Developing services for the sign-up activity for web applications

  • Developing services for the sign-in activity for web applications

  • Developing services for the Address Book application

  • Deploying web applications on AWS Elastic Beanstalk

Introduction


In the earlier chapters, we learned various things about DynamoDB and worked on various recipes to see how things work. Now, it's time to develop a real-world application step by step, integrating all the pieces together to build an Internet scalable web application.

In this chapter, we are going to see how to build an Address Book web application. The requirements are quite similar to any application that we use these days:

  • A new user should be able to register to the application

  • A registered user should be able to log in to the application

  • A logged-in user should be able to add new contacts

  • A logged-in user should be able to view the already added contacts

We would also like to see our application being hosted on AWS Beanstalk so that any user on the Internet should be able to use this application.

The technology stack for this application would be as follows:

  • Java for backend services

  • Spring MVC for the REST API development

  • DynamoDB for data storage

  • AngularJS for frontend

  • AWS Elastic...

Performing data modeling and table creations


In this recipe, we will see how to model data for this web application and create a DynamoDB table accordingly.

Getting ready

To get started with this recipe, you need to know how to create a table in DynamoDB using the console.

How to do it…

To perform data modeling for any web application using the NoSQL database as a backend, we need to first start with what queries we would like to execute on the tables. Considering the requirements, we will need at least two tables:

  • User: This is a table used to store information of a valid user's details along with the credentials

  • Contact: This is a table used to store information of all the contacts added by the users

As we will use the user table to save the registered user's information, as well as to sign-in, we can use email as the hash key for this table.

Here, we won't need a range key as the e-mail ID should be good enough to recognize a user for its registration and login. So, let's create the user table...

Developing services for the sign-up activity for web applications


In the earlier recipe, we created DynamoDB tables based on the need. Now, we will write services to put the data into DynamoDB and get the data back for our application. In this recipe, we will learn how to write services for the sign-up/user registration activity.

Getting ready

To get started with this recipe, you need to know to use the AWS SDK for DynamoDB.

How to do it…

Let's write services to put the data into DynamoDB:

  1. To get started, we will first create a maven project and add the AWS SDK:

    dependency:<dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-dynamodb</artifactId>
      <version>1.10.4.1</version>
    </dependency>
  2. Next, we will create a model class for the user table. Here, we will use the DynamoDBMapper class to connect to DynamoDB using Object Persistence Model. We will also consider attributes, such as the first name, last name, password, and so on...

Developing services for the sign-in activity for web applications


In the earlier recipe, we wrote and tested services for user registration. In this recipe, we will implement services for the user sign-in.

Getting ready

To get started with this recipe, you should have performed the earlier recipe.

How to do it…

For this recipe, we will use the same project that we created earlier:

  1. To start with, we use the same user model that we defined in the earlier recipe. As we had encrypted the password during registration, it is very important to use the same client, which was initiated by the encryption provider:

    static AmazonDynamoDBClient client = new AmazonDynamoDBClient(
          new ClasspathPropertiesFileCredentialsProvider());
    static DynamoDBMapper mapper = new DynamoDBMapper(client,
      DynamoDBMapperConfig.DEFAULT, new AttributeEncryptor(
          EncryptorProvider.getProvider()));
    static {
    client.setRegion(Region.getRegion(Regions.US_WEST_2));
    }
  2. Next, we write a method which will fetch the user record...

Developing services for the Address Book application


Now that we are done with the sign-up and sign-in activities for our address book application, it's time to get star ted with developing address-related services. In this recipe, we will develop two APIs, one to add a new contact and another one to view all the contacts.

Getting ready

To get started with this recipe, you should have performed the earlier recipe.

How to do it…

For this recipe, we will also use the same project setup that we had earlier:

  1. To get started, we need to first create a model class for a contact record in the contact table. Here, we create the table with the hash key as ID (auto-generated) and the range key as the parent ID (e-mail ID of the user who added this contact):

    @DynamoDBTable(tableName = "contact")
    public class Contact {
      private String id;
      private String parentId;
      private String fname;
      private String lname;
      private String phone;
      private String email;
      @DynamoDBHashKey
      @DynamoDBAutoGeneratedKey...

Deploying web applications on AWS Elastic Beanstalk


AWS Elastic Beanstalk allows you to quickly deploy your applications on Cloud, and they can be made accessible to the public. You can build your applications in languages, such as Java, PHP, Ruby, and so on and deploy them to the application server of your choice, such as Apache Tomcat, PHP, Windows Server, Node.js, and so on.

You can read more about Elastic Beanstalk at http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/Welcome.html.

In this recipe, we will see how to deploy the Address Book application using Elastic Beanstalk and access it from Cloud.

Getting ready

To get started with this recipe, you should already have built your application and tested it locally. There are several ways to deploy the application, but for this recipe, we will use the Eclipse AWS Toolkit. You should also have installed the AWS Toolkit for Eclipse. If you haven't done this yet, you can refer to http://docs.aws.amazon.com/AWSToolkitEclipse/latest/GettingStartedGuide...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
DynamoDB Cookbook
Published in: Sep 2015Publisher: ISBN-13: 9781784393755
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Tanmay Deshpande

Tanmay Deshpande is a Hadoop and big data evangelist. He currently works with Schlumberger as a Big Data Architect in Pune, India. He has interest in a wide range of technologies, such as Hadoop, Hive, Pig, NoSQL databases, Mahout, Sqoop, Java, cloud computing, and so on. He has vast experience in application development in various domains, such as oil and gas, finance, telecom, manufacturing, security, and retail. He enjoys solving machine-learning problems and spends his time reading anything that he can get his hands on. He has great interest in open source technologies and has been promoting them through his talks. Before Schlumberger, he worked with Symantec, Lumiata, and Infosys. Through his innovative thinking and dynamic leadership, he has successfully completed various projects. He regularly blogs on his website http://hadooptutorials.co.in. You can connect with him on LinkedIn at https://www.linkedin.com/in/deshpandetanmay/. He has also authored Mastering DynamoDB, published in August 2014, DynamoDB Cookbook, published in September 2015, Hadoop Real World Solutions Cookbook-Second Edition, published in March 2016, Hadoop: Data Processing and Modelling, published in August, 2016, and Hadoop Blueprints, published in September 2016, all by Packt Publishing.
Read more about Tanmay Deshpande