Märkus.
Juurdepääs sellele lehele nõuab autoriseerimist. Võite proovida sisse logida või kausta vahetada.
Juurdepääs sellele lehele nõuab autoriseerimist. Võite proovida kausta vahetada.
This article shows how to create a sample application that uses Spring Data to store and retrieve information by using Azure Cosmos DB for MongoDB.
Prerequisites
An Azure subscription - create one for free.
Java Development Kit (JDK), version 8 or higher.
- A Git client.
Create an Azure Cosmos DB account
Create an Azure Cosmos DB account by using the Azure portal
Note
For more information about creating accounts, see the Azure Cosmos DB documentation.
Browse to the Azure portal at https://portal.azure.com/ and sign in.
Select Create a resource, select Databases, and then select Azure Cosmos DB.
On the Select API option screen, select Azure Cosmos DB for MongoDB.
Specify the following information:
- Subscription: Specify your Azure subscription to use.
- Resource group: Specify whether to create a new resource group or choose an existing resource group.
- Account name: Choose a unique name for your Azure Cosmos DB account. This name is used to create a fully qualified domain name like
wingtiptoysmongodb.documents.azure.com. - API: Specify
Azure Cosmos DB for MongoDB APIfor this tutorial. - Location: Specify the closest geographic region for your database.
When you enter all of the preceding information, select Review + create.
If everything looks correct on the review page, select Create.
Retrieve the connection string for your Azure Cosmos DB account
Browse to the Azure portal at https://portal.azure.com/ and sign in.
Click All Resources, and then select the Azure Cosmos DB account you created.
Click Connection strings, and copy the value for the Primary Connection String field. Use this value to configure your application later.
Configure the sample application
Open a command shell and clone the sample project by using a git command. For example:
git clone https://github.com/spring-guides/gs-accessing-data-mongodb.gitCreate a resources directory in the <project root>/complete/src/main directory of the sample project. Then, create an application.properties file in the resources directory.
Open the application.properties file in a text editor. Add the following lines to the file, and replace the sample values with the appropriate values from earlier:
spring.data.mongodb.database=wingtiptoysmongodb spring.data.mongodb.uri=mongodb://wingtiptoysmongodb:AbCdEfGhIjKlMnOpQrStUvWxYz==@wingtiptoysmongodb.documents.azure.com:10255/?ssl=true&replicaSet=globaldbWhere:
Parameter Description spring.data.mongodb.databaseSpecifies the name of your Azure Cosmos DB account from earlier in this article. spring.data.mongodb.uriSpecifies the Primary Connection String from earlier in this article. Save and close the application.properties file.
Package and test the sample application
To build the application, go to the directory /gs-accessing-data-mongodb/complete, which contains the pom.xml file.
Build the sample application with Maven, and configure Maven to skip tests; for example:
mvn clean package -DskipTestsStart the sample application; for example:
java -jar target/accessing-data-mongodb-complete-0.0.1-SNAPSHOT.jarYour application should return values like the following:
Customers found with findAll(): ------------------------------- Customer[id=5c1b4ae4d0b5080ac105cc13, firstName='Alice', lastName='Smith'] Customer[id=5c1b4ae4d0b5080ac105cc14, firstName='Bob', lastName='Smith'] Customer found with findByFirstName('Alice'): -------------------------------- Customer[id=5c1b4ae4d0b5080ac105cc13, firstName='Alice', lastName='Smith'] Customers found with findByLastName('Smith'): -------------------------------- Customer[id=5c1b4ae4d0b5080ac105cc13, firstName='Alice', lastName='Smith'] Customer[id=5c1b4ae4d0b5080ac105cc14, firstName='Bob', lastName='Smith']
Summary
In this tutorial, you created a sample Java application that uses Spring Data to store and retrieve information by using Azure Cosmos DB for MongoDB.
Clean up resources
When you no longer need the resources, use the Azure portal to delete them. Deleting the resources helps you avoid unexpected charges.
Next steps
To learn more about Spring and Azure, continue to the Spring on Azure documentation center.
See also
For more information about using Azure with Java, see the Azure for Java Developers and the Working with Azure DevOps and Java.