Enterprise Java

Map DynamoDB Items to Objects using DynamoDB mapper

Previously we created DynamoDB Tables using Java.

For various databases such sql databases or nosql there is a set of tools that help to access, persist, and manage data between objects/classes and the underlying database. For example for SQL databases we use JPA, for Cassandra we use MappingManager.

DynamoDBMapper is a tool that enables you to access your data in various tables, perform various CRUD operations on items, and execute queries and scans against tables.

We will try to map the Users, Logins, Supervisors and companies tables from the previous example.
Users is a simple table using the users email as a Hash key.

package com.gkatzioura.dynamodb.mapper.entities;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

/**
 * Created by gkatzioura on 9/20/16.
 */
@DynamoDBTable(tableName="Users")
public class User {

    private String email;
    private String fullName;

    @DynamoDBHashKey(attributeName="email")
    public String getEmail() {
        return email;
    }

    @DynamoDBAttribute(attributeName="fullname")
    public void setEmail(String email) {
        this.email = email;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
}

However in various cases our DynamoDB Table uses a hash and a range key. The Logins table keeps track of the login attempts of a user. The email is the hash key and the timestamp the range key.

package com.gkatzioura.dynamodb.mapper.entities;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

/**
 * Created by gkatzioura on 9/20/16.
 */
@DynamoDBTable(tableName="Logins")
public class Login {

    private String email;
    private Long timestamp;

    @DynamoDBHashKey(attributeName="email")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @DynamoDBRangeKey(attributeName="timestamp")
    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }
}

Another popular case are tables with global secondary indexes (GSI). For example the Supervisors table is used to retrieve a supervisor by his name. However we also use this table in order to retrieve all the supervisors from a specific company or the supervisors who work on specific factory of a company.
The supervisor name is our hash key, the company name is the hash key and the factory name is the range key of the global secondary index.

package com.gkatzioura.dynamodb.mapper.entities;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

/**
 * Created by gkatzioura on 9/21/16.
 */
@DynamoDBTable(tableName="Supervisors")
public class Supervisor {

    private String name;
    private String company;
    private String factory;

    @DynamoDBHashKey(attributeName="name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @DynamoDBIndexHashKey(globalSecondaryIndexName = "FactoryIndex",attributeName = "company")
    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    @DynamoDBIndexRangeKey(globalSecondaryIndexName = "FactoryIndex",attributeName = "factory")
    public String getFactory() {
        return factory;
    }

    public void setFactory(String factory) {
        this.factory = factory;
    }
}

Last but not least we can use Local Secondary Indexes. The Companies table uses the company name as a hash key and the subsidiary name as a range key. Since we want to issue queries based on a company’s CEOs a local secondary index is used with a range key based on the name of the CEO.

package com.gkatzioura.dynamodb.mapper.entities;

import com.amazonaws.services.dynamodbv2.datamodeling.*;

/**
 * Created by gkatzioura on 9/21/16.
 */
@DynamoDBTable(tableName="Companies")
public class Company {

    private String name;
    private String subsidiary;
    private String ceo;

    @DynamoDBHashKey(attributeName="name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @DynamoDBRangeKey(attributeName = "subsidiary")
    public String getSubsidiary() {
        return subsidiary;
    }

    public void setSubsidiary(String subsidiary) {
        this.subsidiary = subsidiary;
    }

    @DynamoDBIndexRangeKey(localSecondaryIndexName = "CeoIndex",attributeName = "ceo")
    public String getCeo() {
        return ceo;
    }

    public void setCeo(String ceo) {
        this.ceo = ceo;
    }
}

You can find the source code on github.

Reference: Map DynamoDB Items to Objects using DynamoDB mapper from our JCG partner Emmanouil Gkatziouras at the gkatzioura blog.

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button