Skip to content

Latest commit

 

History

History
251 lines (184 loc) · 11.2 KB

spring-data-support.md

File metadata and controls

251 lines (184 loc) · 11.2 KB
titledescriptionms.dateauthorms.authorms.reviewerms.topicms.customappliesto
Spring Data support
This article describes how Spring Cloud Azure and Spring Data can be used together.
08/10/2023
KarlErickson
karler
seal
reference
devx-track-java, devx-track-extended-java
✅ Version 4.20.0
✅ Version 5.22.0

Spring Data support

This article describes how Spring Cloud Azure and Spring Data can be used together.

Spring Data Azure Cosmos DB support

Azure Cosmos DB is a globally distributed database service that allows developers to work with data using various standard APIs, such as SQL, MongoDB, Graph, and Azure Table storage.

Dependency setup

<dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-data-cosmos</artifactId> </dependency>

Configuration

Note

If you use a security principal to authenticate and authorize with Microsoft Entra ID for accessing an Azure resource, be sure the security principal has been granted sufficient permission to access the Azure resource. For more information, see Authorize access with Microsoft Entra ID.

The following table lists the configurable properties of spring-cloud-azure-starter-data-cosmos:

[!div class="mx-tdBreakAll"]

PropertyDescription
spring.cloud.azure.cosmos.enabledA value that indicates whether Azure Cosmos DB Service is enabled. The default value is true.
spring.cloud.azure.cosmos.databaseThe Azure Cosmos DB database ID.
spring.cloud.azure.cosmos.endpointThe URI to connect Azure Cosmos DB.
spring.cloud.azure.cosmos.keyThe PrivateKey to connect Azure Cosmos DB.
spring.cloud.azure.cosmos.credential.client-certificate-passwordThe password of the certificate file.
spring.cloud.azure.cosmos.credential.client-certificate-pathThe path of a PEM certificate file to use when performing service principal authentication with Azure.
spring.cloud.azure.cosmos.credential.client-idThe client ID to use when performing service principal authentication with Azure.
spring.cloud.azure.cosmos.credential.client-secretThe client secret to use when performing service principal authentication with Azure.
spring.cloud.azure.cosmos.credential.managed-identity-enabledWhether to enable managed identity. The default value is false.
spring.cloud.azure.cosmos.credential.passwordThe password to use when performing username/password authentication with Azure.
spring.cloud.azure.cosmos.credential.usernameThe username to use when performing username/password authentication with Azure.
spring.cloud.azure.cosmos.populate-query-metricsA value that indicates whether to populate diagnostics strings and query metrics. The default value is false.
spring.cloud.azure.cosmos.consistency-levelA consistency level for Azure Cosmos DB.

Key concepts

The following list shows the key concepts of the Spring Data support:

  • The Spring Data CrudRepository and ReactiveCrudRepository, which provide the following basic CRUD functionality:

    • save
    • findAll
    • findOne by ID
    • deleteAll
    • delete by ID
    • delete entity
  • The Spring Data @Id annotation. There are two ways to map a field in a domain class to the id of an Azure Cosmos DB document:

    • Annotate a field in domain class with @Id. This field will be mapped to document id in Azure Cosmos DB.
    • Set the name of this field to id. This field will be mapped to document id in Azure Cosmos DB.

    [!NOTE] If both ways are applied, the @Id annotation has higher priority.

  • Custom collection names. 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. To specify a field of your domain class to be a partition key field, annotate it with @PartitionKey. When you do CRUD operations, specify your partition value. For more examples, see AddressRepositoryIT.java on GitHub.

  • Supports Spring Data custom query find operation.

  • Supports spring-boot-starter-data-rest.

  • Supports List and nested types in domain classes.

Basic usage

Use a private key to access Azure Cosmos DB

The simplest way to connect Azure Cosmos DB with spring-cloud-azure-starter-data-cosmos is with a primary key. Add the following properties:

spring: cloud: azure: cosmos: key: ${AZURE_COSMOS_KEY}endpoint: ${AZURE_COSMOS_ENDPOINT}database: ${AZURE_COSMOS_DATABASE}

Define an entity

Define an entity as a Document in Azure Cosmos DB, as shown in the following example:

@Container(containerName = "mycollection") publicclassUser { @IdprivateStringid; privateStringfirstName; @PartitionKeyprivateStringlastName; privateStringaddress; publicUser() { } publicUser(Stringid, StringfirstName, StringlastName, Stringaddress) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.address = address; } publicStringgetId() { returnid; } publicvoidsetId(Stringid) { this.id = id; } publicStringgetFirstName() { returnfirstName; } publicvoidsetFirstName(StringfirstName) { this.firstName = firstName; } publicStringgetLastName() { returnlastName; } publicvoidsetLastName(StringlastName) { this.lastName = lastName; } publicStringgetAddress() { returnaddress; } publicvoidsetAddress(Stringaddress) { this.address = address; } @OverridepublicStringtoString() { returnString.format("%s %s, %s", firstName, lastName, address); } }

The id field will be used as the document id in Azure Cosmos DB. Alternately, you can annotate any field with @Id to map it to the document id.

The annotation @Container(containerName = "mycollection") is used to specify the collection name of your document in Azure Cosmos DB.

Create repositories

To create repositories, extend the ReactiveCosmosRepository interface, which provides Spring Data repository support.

@RepositorypublicinterfaceUserRepositoryextendsReactiveCosmosRepository<User, String> { Flux<User> findByFirstName(StringfirstName); }

Currently, the ReactiveCosmosRepository interface provides basic save, delete, and find operations. More operations will be supported later.

Create an application class

The following example creates an application class with all the components:

@SpringBootApplicationpublicclassCosmosSampleApplicationimplementsCommandLineRunner { privatestaticfinalLoggerLOGGER = LoggerFactory.getLogger(CosmosSampleApplication.class); @AutowiredprivateUserRepositoryrepository; @AutowiredprivateCosmosPropertiesproperties; publicstaticvoidmain(String[] args) { SpringApplication.run(CosmosSampleApplication.class, args); } publicvoidrun(String... var1) { finalUsertestUser = newUser("testId", "testFirstName", "testLastName", "test address line one"); // Save the User class to Azure Cosmos DB database.finalMono<User> saveUserMono = repository.save(testUser); finalFlux<User> firstNameUserFlux = repository.findByFirstName("testFirstName"); // Nothing happens until we subscribe to these Monos.// findById won't return the user as user isn't present.finalMono<User> findByIdMono = repository.findById(testUser.getId()); finalUserfindByIdUser = findByIdMono.block(); Assert.isNull(findByIdUser, "User must be null"); finalUsersavedUser = 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(); finalOptional<User> optionalUserResult = repository.findById(testUser.getId()).blockOptional(); Assert.isTrue(optionalUserResult.isPresent(), "Cannot find user."); finalUserresult = 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()); } @PostConstructpublicvoidsetup() { // For this example, remove all of the existing records.this.repository.deleteAll().block(); } }

This example includes an autowired UserRepository interface to support save, delete, and find operations.

Samples

See the azure-spring-boot-samples on GitHub.

Apart from using the spring-cloud-azure-starter-data-cosmos library, you can directly use azure-spring-data-cosmos library for more complex scenarios. For more information, see Spring Data for Azure Cosmos DB client library.

close