-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Spring Boot migration -- CosmosDB and Gremlin #11032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
48d93f3
Merge pull request #2 from Azure/master
yiliuTo 6a2635b
migrate cosmosdb and gremlin
yiliuTo 7cbce84
add code comment
yiliuTo 4f87876
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
yiliuTo 3620af1
delete lombok and unused test code
yiliuTo d123069
modify readme for version tag
yiliuTo 590619f
modify readme for version tag and add example
yiliuTo 0eb76da
add include lib for enforcer
yiliuTo c9b4546
Merge branch 'feature/cosgrem' of https://github.com/yiliuTo/azure-sd…
yiliuTo 8bb6140
modify for checkstyle
yiliuTo 11b588d
add CHANGELOG.md
yiliuTo 95a305d
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
yiliuTo 9d4648e
add entries in jacoco and plugin for javadoc
yiliuTo efc5541
Merge branch 'master' into feature/cosgrem
saragluna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
sdk/spring/azure-spring-boot-starter-cosmosdb/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| ## Azure Cosmos DB Spring Boot Starter client library for Java | ||
|
|
||
| [Azure Cosmos DB](https://azure.microsoft.com/services/cosmos-db/) is a globally-distributed database service that allows developers to work with data using a variety of standard APIs, such as SQL, MongoDB, Graph, and Azure Table storage. | ||
|
|
||
| ## TOC | ||
|
|
||
| * [Key concepts](#key-concepts) | ||
| * [Examples](#examples) | ||
| * [Getting started](#getting-started) | ||
|
|
||
| ## Key concepts | ||
| - Spring Data ReactiveCrudRepository basic CRUD functionality | ||
| - save | ||
| - findAll | ||
| - findOne by Id | ||
| - deleteAll | ||
| - delete by Id | ||
| - delete entity | ||
| - Spring Data [@Id](https://github.com/spring-projects/spring-data-commons/blob/db62390de90c93a78743c97cc2cc9ccd964994a5/src/main/java/org/springframework/data/annotation/Id.java) annotation. | ||
| There're 2 ways to map a field in domain class to `id` of Azure Cosmos DB document. | ||
| - annotate a field in domain class with @Id, this field will be mapped to document `id` in Cosmos DB. | ||
| - set name of this field to `id`, this field will be mapped to document `id` in Cosmos DB. | ||
| [Note] if both way applied, | ||
| - Custom collection Name. | ||
| By default, collection name will be class name of user domain class. To customize it, add annotation `@Document(collection="myCustomCollectionName")` to your domain class, that's all. | ||
| - Supports [Azure Cosmos DB partition](https://docs.microsoft.com/azure/cosmos-db/partition-data). To specify a field of your domain class to be partition key field, just annotate it with `@PartitionKey`. When you do CRUD operation, please specify your partition value. For more sample on partition CRUD, please refer to [test here](./test/java/com/microsoft/azure/spring/data/cosmosdb/repository/AddressRepositoryIT.java) | ||
| - Supports [Spring Data custom query](https://docs.spring.io/spring-data/commons/docs/current/reference/html/#repositories.query-methods.details) find operation. | ||
| - Supports [spring-boot-starter-data-rest](https://projects.spring.io/spring-data-rest/). | ||
| - Supports List and nested type in domain class. | ||
|
|
||
| ## Examples | ||
| Please refer to [sample project here](../azure-spring-boot-samples/azure-spring-boot-sample-cosmosdb). | ||
|
|
||
| ## Getting started | ||
|
|
||
| ### Add the dependency | ||
|
|
||
| `azure-cosmosdb-spring-boot-starter` is published on Maven Central Repository. | ||
| If you are using Maven, add the following dependency. | ||
|
|
||
| [//]: # ({x-version-update-start;com.azure:azure-cosmosdb-spring-boot-starter;current}) | ||
| ```xml | ||
| <dependency> | ||
| <groupId>com.azure</groupId> | ||
| <artifactId>azure-cosmosdb-spring-boot-starter</artifactId> | ||
| <version>2.2.5-beta.1</version> | ||
| </dependency> | ||
| ``` | ||
| [//]: # ({x-version-update-end}) | ||
|
|
||
| ### Add the property setting | ||
|
|
||
| Open `application.properties` file and add below properties with your Cosmos DB credentials. | ||
|
|
||
| ```properties | ||
| azure.cosmosdb.uri=your-cosmosdb-uri | ||
| azure.cosmosdb.key=your-cosmosdb-key | ||
| azure.cosmosdb.database=your-cosmosdb-databasename | ||
| ``` | ||
|
|
||
| Property `azure.cosmosdb.consistency-level` is also supported. | ||
|
|
||
| Property `azure.cosmosdb.cosmosKeyCredential` is also supported. CosmosKeyCredential feature provides capability to | ||
| rotate keys on the fly. You can switch keys using switchToSecondaryKey(). For more information on this, see the Sample | ||
| Application code. | ||
|
|
||
| ### Define an entity | ||
| Define a simple entity as Document in Cosmos DB. | ||
| <!-- embedme ../azure-spring-boot/src/samples/java/com/azure/spring/cosmosdb/User.java#L10-L65 --> | ||
| ```java | ||
| @Document(collection = "mycollection") | ||
| public class User { | ||
| @Id | ||
| private String id; | ||
| private String firstName; | ||
| @PartitionKey | ||
| private String lastName; | ||
| private String address; | ||
|
|
||
| public User() { | ||
| } | ||
|
|
||
| public User(String id, String firstName, String lastName, String address) { | ||
| this.id = id; | ||
| this.firstName = firstName; | ||
| this.lastName = lastName; | ||
| this.address = address; | ||
| } | ||
|
|
||
| public String getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(String id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getFirstName() { | ||
| return firstName; | ||
| } | ||
|
|
||
| public void setFirstName(String firstName) { | ||
| this.firstName = firstName; | ||
| } | ||
|
|
||
| public String getLastName() { | ||
| return lastName; | ||
| } | ||
|
|
||
| public void setLastName(String lastName) { | ||
| this.lastName = lastName; | ||
| } | ||
|
|
||
| public String getAddress() { | ||
| return address; | ||
| } | ||
|
|
||
| public void setAddress(String address) { | ||
| this.address = address; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format("%s %s, %s", firstName, lastName, address); | ||
| } | ||
| } | ||
| ``` | ||
| `id` field will be used as document `id` in Azure Cosmos DB. Or you can annotate any field with `@Id` to map it to document `id`. | ||
|
|
||
| Annotation `@Document(collection="mycollection")` is used to specify the collection name of your document in Azure Cosmos DB. | ||
|
|
||
| ### Create repositories | ||
| Extends ReactiveCosmosRepository interface, which provides Spring Data repository support. | ||
| <!-- embedme ../azure-spring-boot/src/samples/java/com/azure/spring/cosmosdb/UserRepository.java#L10-L14 --> | ||
| ```java | ||
| @Repository | ||
| public interface UserRepository extends ReactiveCosmosRepository<User, String> { | ||
|
|
||
| Flux<User> findByFirstName(String firstName); | ||
| } | ||
| ``` | ||
|
|
||
| So far ReactiveCosmosRepository provides basic save, delete and find operations. More operations will be supported later. | ||
|
|
||
| ### Create an Application class | ||
| Here create an application class with all the components | ||
| <!-- embedme ../azure-spring-boot/src/samples/java/com/azure/spring/cosmosdb/CosmosSampleApplication.java#L18-L65 --> | ||
| ```java | ||
| @SpringBootApplication | ||
| public class CosmosSampleApplication implements CommandLineRunner { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(CosmosSampleApplication.class); | ||
|
|
||
| @Autowired | ||
| private UserRepository repository; | ||
|
|
||
| public static void main(String[] args) { | ||
| SpringApplication.run(CosmosSampleApplication.class, args); | ||
| } | ||
|
|
||
| public void run(String... var1) { | ||
| final User testUser = new User("testId", "testFirstName", "testLastName", "test address line one"); | ||
|
|
||
| // Save the User class to Azure CosmosDB database. | ||
| final Mono<User> saveUserMono = repository.save(testUser); | ||
|
|
||
| final Flux<User> firstNameUserFlux = repository.findByFirstName("testFirstName"); | ||
|
|
||
| // Nothing happens until we subscribe to these Monos. | ||
| // findById will not return the user as user is not present. | ||
| final Mono<User> findByIdMono = repository.findById(testUser.getId()); | ||
| final User findByIdUser = findByIdMono.block(); | ||
| Assert.isNull(findByIdUser, "User must be null"); | ||
|
|
||
| final User savedUser = saveUserMono.block(); | ||
| Assert.state(savedUser != null, "Saved user must not be null"); | ||
| Assert.state(savedUser.getFirstName().equals(testUser.getFirstName()), "Saved user first name doesn't match"); | ||
|
|
||
| firstNameUserFlux.collectList().block(); | ||
|
|
||
| final Optional<User> optionalUserResult = repository.findById(testUser.getId()).blockOptional(); | ||
| Assert.isTrue(optionalUserResult.isPresent(), "Cannot find user."); | ||
|
|
||
| final User result = optionalUserResult.get(); | ||
| Assert.state(result.getFirstName().equals(testUser.getFirstName()), "query result firstName doesn't match!"); | ||
| Assert.state(result.getLastName().equals(testUser.getLastName()), "query result lastName doesn't match!"); | ||
|
|
||
| LOGGER.info("findOne in User collection get result: {}", result.toString()); | ||
| } | ||
|
|
||
| @PostConstruct | ||
| public void setup() { | ||
| // For this example, remove all of the existing records. | ||
| this.repository.deleteAll().block(); | ||
| } | ||
| } | ||
| ``` | ||
| Autowired UserRepository interface, then can do save, delete and find operations. | ||
|
|
||
| ### Allow telemetry | ||
| Microsoft would like to collect data about how users use this Spring boot starter. Microsoft uses this information to improve our tooling experience. Participation is voluntary. If you don't want to participate, just simply disable it by setting below configuration in `application.properties`. | ||
| ```properties | ||
| azure.cosmosdb.allow-telemetry=false | ||
| ``` | ||
| When telemetry is enabled, an HTTP request will be sent to URL `https://dc.services.visualstudio.com/v2/track`. So please make sure it's not blocked by your firewall. | ||
| Find more information about Azure Service Privacy Statement, please check [Microsoft Online Services Privacy Statement](https://www.microsoft.com/privacystatement/OnlineServices/Default.aspx). | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ## Next steps | ||
|
|
||
| Besides using this Azure CosmosDb Spring Boot Starter, you can directly use Spring Data for Azure CosmosDb package for more complex scenarios. Please refer to [Spring Data for Azure CosmosDB](https://github.com/Microsoft/spring-data-cosmosdb) for more details. | ||
|
|
||
| ## Contributing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <parent> | ||
| <groupId>com.azure</groupId> | ||
| <artifactId>azure-client-sdk-parent</artifactId> | ||
| <version>1.7.0</version> <!-- {x-version-update;com.azure:azure-client-sdk-parent;current} --> | ||
| <relativePath>../../parents/azure-client-sdk-parent</relativePath> | ||
| </parent> | ||
|
|
||
| <groupId>com.microsoft.azure</groupId> | ||
| <artifactId>azure-cosmosdb-spring-boot-starter</artifactId> | ||
| <version>2.2.5-beta.1</version> <!-- {x-version-update;com.microsoft.azure:azure-cosmosdb-spring-boot-starter;current} --> | ||
|
|
||
| <name>Azure Cosmos DB Spring Boot Starter</name> | ||
| <description>Spring Boot Starter for Azure Document DB service</description> | ||
| <url>https://github.com/Azure/azure-sdk-for-java</url> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter</artifactId> | ||
| <version>2.2.0.RELEASE</version> <!-- {x-version-update;org.springframework.boot:spring-boot-starter;external_dependency} --> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-validation</artifactId> | ||
| <version>2.2.0.RELEASE</version> <!-- {x-version-update;org.springframework.boot:spring-boot-starter-validation;external_dependency} --> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.microsoft.azure</groupId> | ||
| <artifactId>azure-spring-boot</artifactId> | ||
| <version>2.2.5-beta.1</version> <!-- {x-version-update;com.microsoft.azure:azure-spring-boot;current} --> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.microsoft.azure</groupId> | ||
| <artifactId>spring-data-cosmosdb</artifactId> | ||
| <version>2.2.3.FIX1</version> <!-- {x-version-update;com.microsoft.azure:spring-data-cosmosdb;external_dependency} --> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-enforcer-plugin</artifactId> | ||
| <version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-enforcer-plugin;external_dependency} --> | ||
| <configuration> | ||
| <rules> | ||
| <bannedDependencies> | ||
| <includes> | ||
| <include>com.microsoft.azure:*</include> | ||
| <include>org.springframework.boot:spring-boot-starter:[2.2.0.RELEASE]</include> <!-- {x-include-update;org.springframework.boot:spring-boot-starter;external_dependency} --> | ||
| <include>org.springframework.boot:spring-boot-starter-validation:[2.2.0.RELEASE]</include> <!-- {x-include-update;org.springframework.boot:spring-boot-starter-validation;external_dependency} --> | ||
| </includes> | ||
| </bannedDependencies> | ||
| </rules> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
1 change: 1 addition & 0 deletions
1
sdk/spring/azure-spring-boot-starter-cosmosdb/src/main/resources/cosmosdb.enable.config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| dummy |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should probably be an entry here for each spring library being added to version_client.txt below.