AWS CLI
Install
pip install awscli
Start DB
When doing local testing, made sure to add the flag -shareDd, otherwise it will show you the ResourceNotFoundException when your application attempt to operate with dynamoDB.
java -Djava.library.path=./DynamoDBLocal_lib -jar /Users/mingch/Documents/apache/dynamodb_local_2016-05-17/DynamoDBLocal.jar -inMemory -sharedDb
Initializing DynamoDB Local with the following configuration:
Port: 8000
InMemory: true
DbPath: null
SharedDb: true
shouldDelayTransientStatuses: false
CorsParams: *
Create Table
aws dynamodb create-table --table-name User --attribute-definitions AttributeName=Id,AttributeType=S AttributeName=FirstName,AttributeType=S --key-schema AttributeName=Id,KeyType=HASH AttributeName=FirstName,KeyType=RANGE --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 --endpoint-url http://localhost:8000
Check local database tables
aws dynamodb list-tables --endpoint-url http://localhost:8000
Create Items
aws dynamodb put-item --table-name User --item '{ "Id" : {"S": "1"} , "FirstName": {"S":"John"}}' --endpoint-url http://localhost:8000 aws dynamodb put-item --table-name User --item '{ "Id" : {"S": "1"} , "FirstName": {"S":"Peter"}}' --endpoint-url http://localhost:8000
Build.Gradle
buildscript { ext { springBootVersion = '1.4.3.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse-wtp' apply plugin: 'org.springframework.boot' apply plugin: 'war' war { baseName = 'codeomitted' version = '0.0.1-SNAPSHOT' } sourceCompatibility = 1.8 repositories { mavenCentral() } configurations { providedRuntime } dependencies { compile('org.springframework.boot:spring-boot-starter') compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') compileOnly('org.projectlombok:lombok') compile('com.github.derjust:spring-data-dynamodb:4.4.1') compile('org.apache.commons:commons-lang3:3.5') }
DynamoDBConfig.java
@Configuration @EnableDynamoDBRepositories(basePackages = "com.codeomitted.dao") 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(AWSCredentials amazonAWSCredentials) { AmazonDynamoDB amazonDynamoDB = new AmazonDynamoDBClient(amazonAWSCredentials); if (StringUtils.isNotEmpty(amazonDynamoDBEndpoint)) { amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint); } return amazonDynamoDB; } @Bean public AWSCredentials amazonAWSCredentials() { return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey); } }
User.java
import lombok.Data; @DynamoDBTable(tableName = "User") public @Data class User { @DynamoDBHashKey(attributeName = "Id") @DynamoDBAutoGeneratedKey private String id; @DynamoDBAttribute(attributeName = "FirstName") private String firstName; //getter and setter generate by lombok }
UserService.java
public interface UserService { public User addUser(User user); public List<User> listUser(); }
UserServiceImpl.java
@Service public class UserServiceImpl implements UserService { @Autowired private UserDAO userDAO; @Override public User addUser(User user) { return userDAO.save(user); } @Override public List<User> listUser() { List<User> result = (List<User>) userDAO.findAll(); return result; } }
UserDAO.java
@EnableScan public interface UserDAO extends CrudRepository<User, String> { List<User> findByFirstName(String firstName); }
UserController.java
@RestController public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/add/user") public User addUser() { User user = new User(); user.setFirstName("Mingch"); user = userService.addUser(user); return user; } @RequestMapping(value = "/list/user") public List<User> listUser() { List<User> list = userService.listUser(); return list; } }
Test Result – Add User
Test Result – List User
Reference
http://docs.aws.amazon.com/cli/latest/reference/dynamodb/create-table.html
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.CLI.html
http://serebrov.github.io/html/2015-02-01-dynamodb-local.html
AWS DynamoDB SpringBoot Tutorial
Why wouldn’t you add a Spring Boot main class to your tutorial?
Main class has nothing important inside. It’s generated by STS.
where is the DAOImpl?
You don’t need that
oh Quick reply…thanks a lot
Also no code is been used for insert..how does this happen?
from UserServiceImpl you call this one –> userDAO.save(user);
It’s Spring Data magic. It just need interface.