Git Product home page Git Product logo

spring-data-dynamodb's Introduction

codecov.io Build Status Maven Central Gitter chat Donation badge

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 a data access layer built on AWS DynamoDB.

Technical infos can be found on the project page.

Supported Features

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-examples.

Quick Start

Download the JAR though Maven Central (SNAPSHOT builds are available via the OSSRH snapshot repository ):

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

Setup DynamoDB configuration as well as enabling Spring-Data DynamoDB repository support via Annotation (XML-based configuration)

@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);
	}

}

Create a DynamoDB entity for this table:

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

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

  @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;
  }
}

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
  Page<User> findAll(Pageable pageable);
}

And finally write a test client

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { 
    PropertyPlaceholderAutoConfiguration.class, DynamoDBConfig.class})
    public class UserRepositoryIntegrationTest {
       
    @Autowired
    UserRepository repository;
        
    @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));
    }
    
    private static final long CAPACITY = 5L;
    
    @Autowired
    private AmazonDynamoDB amazonDynamoDB;
    
    @Before
    public void init() throws Exception {
        // Delete User table in case it exists
        amazonDynamoDB.listTables().getTableNames().stream().
                filter(tableName -> tableName.equals(User.TABLE_NAME)).forEach(tableName -> {
            amazonDynamoDB.deleteTable(tableName);
        });
	
	//Create User table
        amazonDynamoDB.createTable(new DynamoDBMapper(amazonDynamoDB)
            .generateCreateTableRequest(User.class)
	    .withProvisionedThroughput(new ProvisionedThroughput(CAPACITY, CAPACITY)));
    }
    
}

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 Boot compatibility Spring Framework compatibility Spring Data compatibility
1.0.x >= 3.1 && < 4.2
4.2.x >= 1.3.0 && < 1.4.0 >= 4.2 && < 4.3 Gosling-SR1
4.3.x >= 1.4.0 < 2.0 >= 4.3 && < 5.0 Gosling-SR1
4.4.x >= 1.4.0 < 2.0 >= 4.3 && < 5.0 Hopper-SR2
4.5.x >= 1.4.0 < 2.0 >= 4.3 && < 5.0 Ingalls
5.0.x >= 2.0 >= 5.0 Kay-SR1

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.

History

The code base has some history already in it - let's clarify it a bit:

The Java package name/XSD namespace never changed from org.socialsignin.spring.data.dynamodb. But the XSD is now also available at https://derjust.github.io/spring-data-dynamodb/spring-dynamodb-1.0.xsd.

Advanced topics

Advanced topics can be found in the wiki.

spring-data-dynamodb's People

Contributors

derjust avatar michaellavelle avatar alexarana avatar blx avatar cmleroy avatar davinpidoto avatar gauravbrills avatar mtedone avatar matthias-hampel avatar dasniko avatar treidel avatar vitolimandibhrata avatar daquino avatar paulatbox avatar srekapalli avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.