Merge pull request #9771 from ankurguptajec/master

BAEL-4025 : Spring Data Azure Cosmos DB
This commit is contained in:
rpvilao 2020-08-17 14:25:06 +01:00 committed by GitHub
commit 27592171ba
10 changed files with 269 additions and 0 deletions

View File

@ -55,6 +55,7 @@
<module>spring-boot-persistence-mongodb</module>
<module>spring-data-cassandra</module>
<module>spring-data-cassandra-reactive</module>
<module>spring-data-cosmosdb</module>
<module>spring-data-couchbase-2</module>
<module>spring-data-dynamodb</module>
<module>spring-data-eclipselink</module>

View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-cosmosdb</artifactId>
<name>spring-data-cosmos-db</name>
<description>tutorial for spring-data-cosmosdb</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<properties>
<java.version>1.8</java.version>
<cosmodb.version>2.3.0</cosmodb.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>spring-data-cosmosdb</artifactId>
<version>${cosmodb.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,12 @@
package com.baeldung.spring.data.cosmosdb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AzureCosmosDbApplication {
public static void main(String[] args) {
SpringApplication.run(AzureCosmosDbApplication.class, args);
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.spring.data.cosmosdb.config;
import com.azure.data.cosmos.CosmosKeyCredential;
import com.microsoft.azure.spring.data.cosmosdb.config.AbstractCosmosConfiguration;
import com.microsoft.azure.spring.data.cosmosdb.config.CosmosDBConfig;
import com.microsoft.azure.spring.data.cosmosdb.repository.config.EnableCosmosRepositories;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCosmosRepositories(basePackages = "com.baeldung.spring.data.cosmosdb.repository")
public class AzureCosmosDbConfiguration extends AbstractCosmosConfiguration {
@Value("${azure.cosmosdb.uri}")
private String uri;
@Value("${azure.cosmosdb.key}")
private String key;
@Value("${azure.cosmosdb.secondaryKey}")
private String secondaryKey;
@Value("${azure.cosmosdb.database}")
private String dbName;
private CosmosKeyCredential cosmosKeyCredential;
@Bean
public CosmosDBConfig getConfig() {
this.cosmosKeyCredential = new CosmosKeyCredential(key);
CosmosDBConfig cosmosdbConfig = CosmosDBConfig.builder(uri, this.cosmosKeyCredential, dbName)
.build();
return cosmosdbConfig;
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.spring.data.cosmosdb.controller;
import com.baeldung.spring.data.cosmosdb.entity.Product;
import com.baeldung.spring.data.cosmosdb.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/products")
public class ProductController {
private ProductService productService;
@Autowired
public ProductController(ProductService productService) {
this.productService = productService;
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody Product product) {
productService.saveProduct(product);
}
@GetMapping(value = "/{id}/category/{category}")
public Optional<Product> get(@PathVariable String id, @PathVariable String category) {
return productService.findById(id, category);
}
@DeleteMapping(value = "/{id}/category/{category}")
public void delete(@PathVariable String id, @PathVariable String category) {
productService.delete(id, category);
}
@GetMapping
public List<Product> getByName(@RequestParam String name) {
return productService.findProductByName(name);
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.spring.data.cosmosdb.entity;
import com.microsoft.azure.spring.data.cosmosdb.core.mapping.Document;
import com.microsoft.azure.spring.data.cosmosdb.core.mapping.PartitionKey;
import org.springframework.data.annotation.Id;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Document(collection = "products")
public class Product {
@Id
private String productid;
private String productName;
private double price;
@PartitionKey
private String productCategory;
}

View File

@ -0,0 +1,14 @@
package com.baeldung.spring.data.cosmosdb.repository;
import com.baeldung.spring.data.cosmosdb.entity.Product;
import com.microsoft.azure.spring.data.cosmosdb.repository.CosmosRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ProductRepository extends CosmosRepository<Product, String> {
List<Product> findByProductName(String productName);
}

View File

@ -0,0 +1,38 @@
package com.baeldung.spring.data.cosmosdb.service;
import com.azure.data.cosmos.PartitionKey;
import com.baeldung.spring.data.cosmosdb.entity.Product;
import com.baeldung.spring.data.cosmosdb.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Optional;
@Component
public class ProductService {
private ProductRepository repository;
@Autowired
public ProductService(ProductRepository repository) {
this.repository = repository;
}
public List<Product> findProductByName(String productName) {
return repository.findByProductName(productName);
}
public Optional<Product> findById(String productId, String category) {
return repository.findById(productId, new PartitionKey(category));
}
public void saveProduct(Product product) {
repository.save(product);
}
public void delete(String productId, String category) {
repository.deleteById(productId, new PartitionKey(category));
}
}

View File

@ -0,0 +1,4 @@
azure.cosmosdb.uri=cosmodb-uri
azure.cosmosdb.key=cosmodb-primary-key
azure.cosmosdb.secondaryKey=cosmodb-second-key
azure.cosmosdb.database=cosmodb-name

View File

@ -0,0 +1,33 @@
package com.baeldung.spring.data.cosmosdb;
import com.azure.data.cosmos.PartitionKey;
import com.baeldung.spring.data.cosmosdb.entity.Product;
import com.baeldung.spring.data.cosmosdb.repository.ProductRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.Assert;
@SpringBootTest
public class AzureCosmosDbApplicationManualTest {
@Autowired
ProductRepository productRepository;
@Test
public void givenProductIsCreated_whenCallFindById_thenProductIsFound() {
Product product = new Product();
product.setProductid("1001");
product.setProductCategory("Shirt");
product.setPrice(110.0);
product.setProductName("Blue Shirt");
productRepository.save(product);
Product retrievedProduct = productRepository.findById("1001", new PartitionKey("Shirt"))
.orElse(null);
Assert.notNull(retrievedProduct, "Retrieved Product is Null");
}
}