Enterprise Java

Insert DynamoDB Items with DynamoDBMapper

In a previous post we used DynamoDBMapper in order to map DynamoDB Tables into Java objects.

When it comes to insert, our actions are pretty much the same but with a more convenient way. In order to insert an item all you have to do is to persist an object using the object mapper

In our case, we will create a User repository that does a simple insert.

package com.gkatzioura.dynamodb.mapper.repository;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.gkatzioura.dynamodb.mapper.entities.User;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by gkatzioura on 9/22/16.
 */
public class UserMapperRepository {

    private DynamoDBMapper dynamoDBMapper;

    public UserMapperRepository(AmazonDynamoDB amazonDynamoDB) {
        dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
    }

    public void insert(User user) {

        dynamoDBMapper.save(user);
    }

}

To persist we just have to create a simple object.

@Test
    public void testInsertUser() {

        User user = new User();
        user.setRegisterDate(new Date().getTime());
        user.setFullName("John Doe");
        user.setEmail("john@doe.com");

        userMapperRepository.insert(user);
    }

Also using DynamoDBMapper we can do batch inserts or batch deletes. Therefore we will add two extra methods to the repository.

public void insert(List<User> users) {

        dynamoDBMapper.batchWrite(users,new ArrayList<>());
    }

    public void delete(List<User> users) {
        dynamoDBMapper.batchDelete(users);
    }

Adding items in batch (or deleting) them, simply requires to pass a list of objects that contains values for the keys defined.

@Test
    public void testBatchUserInsert() {

        List<User> users = new ArrayList<>();

        for(int i=0;i<10;i++) {

            String email = emailPrefix+i+"@doe.com";
            User user = new User();
            user.setRegisterDate(new Date().getTime());
            user.setFullName("John Doe");
            user.setEmail("john@doe.com");
            users.add(user);
        }

        userMapperRepository.insert(users);
    }

    @Test
    public void testBatchDelete() {

        testBatchUserInsert();

        List<User> users = new ArrayList<>();

        for(int i=0;i<10;i++) {

            String email = emailPrefix+i+"@doe.com";
            User user = new User();
            user.setRegisterDate(new Date().getTime());
            user.setFullName("John Doe");
            user.setEmail("john@doe.com");
            users.add(user);
        }
        
        userMapperRepository.delete(users);
    }

You can find the sourcecode on github

Reference: Insert DynamoDB Items with DynamoDBMapper 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
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button