Testing and Verifying With Local DynamoDB

Problem: 

Need to test code against dynamodb locally without connecting to AWS Cloud DynamoDB. 

Solution Summary: 

First, we will test using dynamodb local. There is also an embedded version of DynamoDB that can be started and stopped programmatically.

You can download, extract and run the local server. Developer guide for the DynamoDB local can be found at http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html.

Prerequisites: 

NA

Solution: 

Test Code

You can wrap the previous data load and scan codes into methods, classes and interfaces, as required and invoke them in order. For instance create table, load table, scan table, delete table.

If you are having a non-Spring boot project, you can do it within your main program. If you are using Spring boot, you can make use of the command line runner:

@SpringBootApplication

public class AwsDynamodbDemoApplication implements CommandLineRunner {

 

    @Autowired

    DataLoader dataLoader;

 

    @Autowired

    ScanService scanService;

 

        public static void main(String[] args) {

                             SpringApplication.run(AwsDynamodbDemoApplication.class, args);

              }

 

    @Override

    public void run(String... strings) throws Exception {

        dataLoader.createTable();

        dataLoader.loadTable();

        final List<Map<String, String>> scanList = scanService.scan();

        System.out.println("SCAN LIST RESULT");

        System.out.println(scanList);

        dataLoader.cleanData();

    }

}

 

You need to specify correct values for the properties used within the code within application.properties file (in case of Spring Boot).

awsDynamoDBRegion=us-east-1

awsDynamoDBEndpoint=http://localhost:8000

demoTableName=Employee

awsAccessKeyId=< your awsAccessKeyId >

awsSecretAccessKey=< your awsSecretAccessKey >

Note: For non, Spring boot applications, you can specify it within any properties file and then import it.

 

This code assumes that the local dynamodb is running on port 8000, which is the default.

 

Embedded Server

You can also use embedded server to start and stop an embedded server from within the code:

For dynamo db local, we need below dependency:

<dependency>

   <groupId>com.amazonaws</groupId>

   <artifactId>DynamoDBLocal</artifactId>

   <version>1.11.0.1</version>

</dependency>

 

Which is available in following repo:

<repository>

    <id>dynamodb-local-oregon</id>

    <name>DynamoDB Local Release Repository</name>

    <url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</url>

</repository>

 

An unspecified dependency needed by dynamodb local is sqllite4java. when sqlite4java initializes, it will not be able to automatically find binary artifacts in the Maven repository, so you either need to add goals to somehow place sqlite4java-*.jar in the same directory with *.dll/*.so/*.jnilib artifacts, or pass sqlite4java.library.path system property to the process using sqlite4java, or use SQLite.setLibraryPath() as the first method you call when working with sqlite4java.

 

For my local testing, I just downloaded sqlite4java-392.zip file, unzipped into a folder in my project root folder and added the location to the specified properties file:

        Properties props = System.getProperties();

        props.setProperty("sqlite4java.library.path", "C:\\Dev\\Tools\\sqlite4java-392");

 

Once dependencies are set, you can create and start the server as:

    DynamoDBProxyServer server = null;

    final String[] localArgs = { "-inMemory" };

    server = ServerRunner.createServerFromCommandLineArgs(localArgs);

    server.start();

 

Server will start on the port 8000.

 

Similarly, you can stop it as:

server.stop();

 

You can even start an embedded version of the server without using http. You may look into the documentation to try it out, but is less recommended that starting on http.

Contact

We learn together, do innovations and then document them at cloudericks.com.

Offline Contact
We connect physically only in Bengaluru currently, but most of our activities happen online. Please follow us here or in social media for more details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About us

Team Cloudericks is a community started by Heartin Kanikathottu to learn about and master cloud computing. Our current focus is on Amazon Web Servivces.

Cloudericks.com aims to document what we learn. We believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream

,
,