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 Steps:
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.
Recent comments