Amazon Dynamo DB With Spring Boot Application

Amazon Dynamo DB With Spring Boot Application

What Is Amazon DynamoDB?
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.

DynamoDB reduce the administrative burdens of operating and scaling a distributed database.

No need to worry about hardware provisioning, setup and configuration, replication, software patching, or cluster scaling.

With DynamoDB, you can create database tables that can store and retrieve any amount of data, and serve any level of request traffic.

You can scale up or scale down your tables’ throughput capacity without downtime or performance degradation, and use the AWS Management Console to monitor resource utilisation and performance metrics.

Allows you to delete expired items from tables automatically to help you reduce storage usage and the cost of storing data that is no longer relevant.

DynamoDB automatically spreads the data and traffic for your tables over a sufficient number of servers to handle your throughput and storage requirements, while maintaining consistent fast performance. All of your data is stored on solid state disks (SSDs) and automatically replicated across multiple Availability Zones in an AWS region, providing built-in high availability and data durability.

What is NoSQL?
NoSQL is a term used to describe high-performance, non-relational databases. NoSQL databases utilize a variety of data models, including document, graph, key-value, and columnar. NoSQL databases are widely recognised for ease of development, scalable performance, high availability, and resilience. Below are several resources are NoSQL databases.

  • MongoDB
  • Amazon DynamoDB
  • Couchbase
  • CouchDB
  • MarkLogic

AWS DynamoDB Supported Platform:
AWS DynamoDB following supported programming languages:

.Net , ColdFusion , Erlang , Groovy , Java

JavaScript , Perl , PHP , Python , Ruby

The Connector module provided by AWS to support various platform.

Advantage of AWS DynamoDB:

  • Event Driven Programming:
    Amazon DynamoDB integrates with AWS Lambda to provide Triggers which enables you to architect applications that automatically react to data changes.
  • Fine-grained Access Control:
    Amazon DynamoDB integrates with AWS Identity and Access Management (IAM) for fine-grained access control for users within your organization. You can assign unique security credentials to each user and control each user’s access to services and resources.
  • Flexible:
    Amazon DynamoDB supports both document and key-value data structures, giving you the flexibility to design the best architecture that is optimal for your application.

DynamoDB in a Spring Boot Application Using Spring Data:

We’ll demonstrate how to configure an application to use a local DynamoDB instance using Spring Data. We’ll also create an example data model and repository class as well as perform actual database operations using an integration test.

  • Maven Dependencies:
    <dependencyManagement >
    <dependencies >
    <dependency>
    <groupId > com.github.derjust </groupId >
    <artifactId > spring-data-dynamodb </artifactId >
    <version> 4.4.1 </version >
    </dependency>
    <dependency>
    <groupId> com.amazonaws </groupId>
    <artifactId> aws-java-sdk </artifactId>
    <version>1.11.157 </version>
    </dependency>
    </dependencies>
  • application.properties file:
    A) Here I have mentioned amazon.dynamodb.endpoint URL this used for the Asia Pacific (Singapore) regoin. It is vary of the all regoins.
    amazon.dynamodb.endpoint=dynamodb.ap-southeast-1.amazonaws.com
    B) How we get the below amazon.aws.accesskey and amazon.aws.secretkey?
    Create Aws account and login account and choose Development Account
    Then click the My Security Credentials below screen will be open then cliking the continue to security credentials.
    amazon.aws.accesskey=xxxxxxx
    amazon.aws.secretkey=xyzxyzxyzxyz

app_security

  • Spring config:
    The properties will be dynamically pulled out of the application.properties file in the Spring config. Below spring configuration for Asia Pacific (Singapore) regoin.
    @Configuration
    @EnableDynamoDBRepositories(basePackages = “com.mitosis”)
    public class DynamoDBConfig {
    @Value(“${amazon.dynamodb.endpoint}”)
    private String amazonDynamoDBEndpoint;
    @Value(“${amazon.aws.accesskey}”)
    private String amazonAWSAccessKey;@Value(“${amazon.aws.secretkey}”)
    private String amazonAWSSecretKey;@Bean
    public AmazonDynamoDB amazonDynamoDB() {
    AmazonDynamoDB amazonDynamoDB = new AmazonDynamoDBClient(amazonAWSCredentials());
    if (!StringUtils.isEmpty(amazonDynamoDBEndpoint)) {
    amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint);
    }
    return amazonDynamoDB;
    }@Bean
    public AWSCredentials amazonAWSCredentials() {
    return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
    }public AmazonDynamoDBClient getClient() {
    BasicAWSCredentials credentials = new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
    AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials).withRegion(Regions.AP_SOUTHEAST_1);
    return client;
    }
  • The Data Model:
    Let’s now create a POJO model to represent the data stored in DynamoDB.
    This POJO will use annotations similar to those used in Hibernate to define the table name, attributes, keys and other aspects of the table.
  • Data Model Attributes:
    The following class, user, represents a table with items that contains 3 attributes:
    id
    firstNme
    lastName
    mobile
  • Java Data Model Class:
    @DynamoDBTable(tableName = “user”)
    public class User {
    private String id;
    private String firstNme;
    private String lastName;
    private Long mobile;@DynamoDBHashKey
    @DynamoDBAutoGeneratedKey
    public String getId() {
    return id;
    }public void setId(String id) {
    this.id = id;
    }@DynamoDBAttribute
    public String getFirstNme() {
    return firstNme;
    }public void setFirstNme(String firstNme) {
    this.firstNme = firstNme;
    }@DynamoDBAttribute
    public String getLastName() {
    return lastName;
    }public void setLastName(String lastName) {
    this.lastName = lastName;
    }@DynamoDBAttribute
    public Long getMobile() {
    return mobile;
    }public void setMobile( Long mobile) {
    this.mobile = mobile;
    }}
  • CRUD Repository:
    Next, we need to create a UserRepository interface to define the CRUD functionality we want to build out. Repositories used to read and persist data to and from DynamoDB will implement this interface:
    @EnableScan
    public interface UserRepository extends CrudRepository<User, String> {User findById(String id);User findByMobile(Long mobile);}
  • Create Table in AWS:

createtab_awsHow to create table in Asia Pacific (Singapore) regoin login choose your regoin first. Goto services choose DynamoDb then click create table refer below image for set table name and perimary id.

  • Run your Application:
    Right click your application from eclipse ot sts tool choose Run As click maven clean. Below image result will come or else if error check your configuration.
    run_instThen as usual run it your application using following command
    maven clean
    maven install
    Run as Sprint Boot App

Recent Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>