Spring Data DynamoDb

Spring Data module providing support for DynamoDb repositories. Forked from: http://github.com/derjust/spring-data-dynamodb

License

License

Categories

Categories

Data
GroupId

GroupId

com.github.igor-g
ArtifactId

ArtifactId

spring-data-dynamodb
Last Version

Last Version

4.5.1
Release Date

Release Date

Type

Type

jar
Description

Description

Spring Data DynamoDb
Spring Data module providing support for DynamoDb repositories. Forked from: http://github.com/derjust/spring-data-dynamodb
Project URL

Project URL

https://github.com/Igor-G/spring-data-dynamodb
Source Code Management

Source Code Management

https://github.com/Igor-G/spring-data-dynamodb

Download spring-data-dynamodb

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.igor-g/spring-data-dynamodb/ -->
<dependency>
    <groupId>com.github.igor-g</groupId>
    <artifactId>spring-data-dynamodb</artifactId>
    <version>4.5.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.igor-g/spring-data-dynamodb/
implementation 'com.github.igor-g:spring-data-dynamodb:4.5.1'
// https://jarcasting.com/artifacts/com.github.igor-g/spring-data-dynamodb/
implementation ("com.github.igor-g:spring-data-dynamodb:4.5.1")
'com.github.igor-g:spring-data-dynamodb:jar:4.5.1'
<dependency org="com.github.igor-g" name="spring-data-dynamodb" rev="4.5.1">
  <artifact name="spring-data-dynamodb" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.igor-g', module='spring-data-dynamodb', version='4.5.1')
)
libraryDependencies += "com.github.igor-g" % "spring-data-dynamodb" % "4.5.1"
[com.github.igor-g/spring-data-dynamodb "4.5.1"]

Dependencies

compile (5)

Group / Artifact Type Version
org.springframework : spring-context jar
org.springframework : spring-tx jar
org.springframework.data : spring-data-commons jar
org.hibernate : hibernate-validator jar 5.2.4.Final
com.amazonaws : aws-java-sdk-dynamodb jar

provided (1)

Group / Artifact Type Version
javax.enterprise : cdi-api jar 1.2

test (4)

Group / Artifact Type Version
org.springframework : spring-test jar
junit : junit jar 4.12
org.mockito : mockito-core jar 1.10.19
ch.qos.logback : logback-classic jar 1.1.7

Project Modules

There are no modules declared in this project.

codecov.io Build Status Maven Central

Spring Data DynamoDB

The primary goal of the Spring Data project is to make it easier to build Spring-powered applications that use data access technologies. This module deals with enhanced support for Amazon DynamoDB based data access layers.

Supported Features

  • Implementation of CRUD methods for DynamoDB Entities
  • Dynamic query generation from query method names (Only a limited number of keywords and comparison operators currently supported)
  • Possibility to integrate custom repository code
  • Easy Spring annotation based integration

Demo application

For a demo of spring-data-dynamodb, using spring-data-rest to showcase DynamoDB repositories exposed with REST, please see spring-data-dynamodb-demo.

Version & Spring Framework compatibility

The major and minor number of this library refers to the compatible Spring framework version. The build number is used as specified by SEMVER.

API changes will follow SEMVER and loosly the Spring Framework releases.

spring-data-dynamodb version Spring Framework compatibility Spring Data compatibility
1.0.x >= 3.1 && < 4.2
4.2.x >= 4.2 && < 4.3 Gosling-SR1
4.3.x >= 4.3 Gosling-SR1
4.4.x >= 4.3 Hopper-SR2
4.5.x >= 4.3 Ingalls

spring-data-dynamodb depends directly on spring-data as also spring-context, spring-data and spring-tx.

compile and runtime dependencies are kept to a minimum to allow easy integartion, for example into Spring-Boot projects.

Quick Start

Download the JAR though Maven:

<dependency>
  <groupId>com.github.derjust</groupId>
  <artifactId>spring-data-dynamodb</artifactId>
  <version>4.5.0</version>
</dependency>

or via Gradle

repositories {
  mavenCentral()
}

dependencies {
  compile group: 'com.github.derjust',
  name: 'spring-data-dynamodb',
  version: '4.5.0'
}

Setup DynamoDB configuration as well as enabling Spring Data DynamoDB repository support.

@Configuration
@EnableDynamoDBRepositories(basePackages = "com.acme.repositories")
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() {
	    // Or use an AWSCredentialsProvider/AWSCredentialsProviderChain
		return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
	}

}

or in XML...

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dynamodb="http://docs.socialsignin.org/schema/data/dynamodb"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://docs.socialsignin.org/schema/data/dynamodb
                           http://docs.socialsignin.org/schema/data/dynamodb/spring-dynamodb.xsd">

  <bean id="amazonDynamoDB" class="com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient">
    <constructor-arg ref="amazonAWSCredentials" />
    <property name="endpoint" value="${amazon.dynamodb.endpoint}" />
  </bean>
  
  <bean id="amazonAWSCredentials" class="com.amazonaws.auth.BasicAWSCredentials">
    <constructor-arg value="${amazon.aws.accesskey}" />
    <constructor-arg value="${amazon.aws.secretkey}" />
  </bean>
  
  <dynamodb:repositories base-package="com.acme.repositories" amazon-dynamodb-ref="amazonDynamoDB" />
  
</beans>

Create a DynamoDB hash-key only table in AWS console, with table name User and with hash key attribute name id.

Create a DynamoDB entity for this table:

@DynamoDBTable(tableName = "User")
public class User {

  private String id;
  private String firstName;
  private String lastName;

  public User() {
  }

  @DynamoDBHashKey
  @DynamoDBAutoGeneratedKey 
  public String getId() {
	return id;
  }

  @DynamoDBAttribute
  public String getFirstName() {
	return firstName;
  }

  @DynamoDBAttribute
  public String getLastName() {
	return lastName;
  }
       
  public void setId(String id) {
    this.id = id;
  }
  
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  @Override
  public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;

      User user = (User) o;

      return id.equals(user.id);
  }

  @Override
  public int hashCode() {
      return id.hashCode();
  }
}

Create a CRUD repository interface in com.acme.repositories:

package com.acme.repositories;

@EnableScan
public interface UserRepository extends CrudRepository<User, String> {
  List<User> findByLastName(String lastName);
}

or for paging and sorting...

package com.acme.repositories;

public interface UserRepository extends PagingAndSortingRepository<User, String> {
  Page<User> findByLastName(String lastName,Pageable pageable);
  
  @EnableScan 
  @EnableScanCount
  public Page<User> findAll(Pageable pageable);
}

And finally write a test client

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { 
    PropertyPlaceholderAutoConfiguration.class, DynamoDBConfig.class})
    public class UserRepositoryIntegrationTest {
     
    private static final String KEY_NAME = "id";
    private static final Long READ_CAPACITY_UNITS = 5L;
    private static final Long WRITE_CAPACITY_UNITS = 5L;
    
    @Autowired
    UserRepository repository;
    
    @Autowired
    private AmazonDynamoDB amazonDynamoDB;
    
    @Before
    public void init() throws Exception {
    
        ListTablesResult listTablesResult = amazonDynamoDB.listTables();
    
        listTablesResult.getTableNames().stream().
                filter(tableName -> tableName.equals(User.TABLE_NAME)).forEach(tableName -> {
            amazonDynamoDB.deleteTable(tableName);
        });
    
        List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition().withAttributeName(KEY_NAME).withAttributeType("S"));
    
        List<KeySchemaElement> keySchemaElements = new ArrayList<KeySchemaElement>();
        keySchemaElements.add(new KeySchemaElement().withAttributeName(KEY_NAME).withKeyType(KeyType.HASH));
    
        CreateTableRequest request = new CreateTableRequest()
                .withTableName(TABLE_NAME)
                .withKeySchema(keySchemaElements)
                .withAttributeDefinitions(attributeDefinitions)
                .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(READ_CAPACITY_UNITS)
                        .withWriteCapacityUnits(WRITE_CAPACITY_UNITS));
    
        amazonDynamoDB.createTable(request);
    
    }
    
    @Test
    public void sampleTestCase() {
        User dave = new User("Dave", "Matthews");
        repository.save(dave);
    
        User carter = new User("Carter", "Beauford");
        repository.save(carter);
    
        List<User> result = repository.findByLastName("Matthews");
        Assert.assertThat(result.size(), is(1));
        Assert.assertThat(result, hasItem(dave));
    }
}

Advanced topics

Advanced topics can be found in the wiki.

Release process

Check pom.xml for the proper <version />, afterwards execute

  $ mvn release:prepare && mvn release:perform

which will tag, build, test and upload the artifacts to Sonatype's OSS staging area.

Then visit https://oss.sonatype.org/#stagingRepositories and close the staging repository.

Afterwards release the staging repository. This will sync with Maven Central (give it some hours to become visible via http://search.maven.org/).

Versions

Version
4.5.1