You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/nosql/quickstart-java-spring-data.md
+20-54Lines changed: 20 additions & 54 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,23 +46,23 @@ Azure Cosmos DB is a multi-model database service that lets you quickly create a
46
46
47
47
* An Azure account with an active subscription.
48
48
* No Azure subscription? You can [try Azure Cosmos DB free](../try-free.md) with no credit card required.
49
-
*[Java Development Kit (JDK) 8](https://www.azul.com/downloads/azure-only/zulu/?&version=java-8-lts&architecture=x86-64-bit&package=jdk). Point your`JAVA_HOME` environment variable to the folder where the JDK is installed.
49
+
*[Java Development Kit (JDK) 8](https://www.azul.com/downloads/azure-only/zulu/?&version=java-8-lts&architecture=x86-64-bit&package=jdk). Set the`JAVA_HOME` environment variable to the JDK install folder.
50
50
* A [Maven binary archive](https://maven.apache.org/download.cgi). On Ubuntu, run `apt-get install maven` to install Maven.
51
51
*[Git](https://www.git-scm.com/downloads). On Ubuntu, run `sudo apt-get install git` to install Git.
52
52
53
53
## Introductory notes
54
54
55
-
*The structure of an Azure Cosmos DB account.* Irrespective of API or programming language, an Azure Cosmos DB *account* contains zero or more *databases*, a *database* (DB) contains zero or more *containers*, and a *container* contains zero or more items, as shown in the diagram below:
55
+
*The structure of an Azure Cosmos DB account.* Irrespective of API or programming language, an Azure Cosmos DB *account* contains zero or more *databases*, a *database* (DB) contains zero or more *containers*, and a *container* contains zero or more items, as shown in the following diagram:
56
56
57
57
:::image type="content" source="../media/account-databases-containers-items/cosmos-entities.png" alt-text="Azure Cosmos DB account entities" border="false":::
58
58
59
59
For more information about databases, containers, and items, see [Azure Cosmos DB resource model](../account-databases-containers-items.md). A few important properties are defined at the level of the container, among them *provisioned throughput* and *partition key*.
60
60
61
-
The provisioned throughput is measured in Request Units (*RUs*) which have a monetary price and are a substantial determining factor in the operating cost of the account. Provisioned throughput can be selected at per-container granularity or per-database granularity, however container-level throughput specification is typically preferred. You may read more about throughput provisioning [here.](../set-throughput.md)
61
+
The provisioned throughput is measured in Request Units (*RUs*) which have a monetary price and are a substantial determining factor in the operating cost of the account. You can select provisioned throughput at per-container granularity or per-database granularity. However, you should prefer container-level throughput specification. For more information, see [Introduction to provisioned throughput in Azure Cosmos DB](../set-throughput.md).
62
62
63
63
As items are inserted into an Azure Cosmos DB container, the database grows horizontally by adding more storage and compute to handle requests. Storage and compute capacity are added in discrete units known as *partitions*. You must choose one field in your documents to be the partition key, which maps each document to a partition.
64
64
65
-
The way partitions are managed is that each partition is assigned a roughly equal slice out of the range of partition key values. For this reason, you should choose a partition key that's relatively random or evenly distributed. Otherwise, some partitions (called *hot partitions*) will see substantially more requests, while other partitions (called *cold partitions*) will see substantially fewer requests, and this is to be avoided. For more information, see [Partitioning and horizontal scaling in Azure Cosmos DB](../partitioning-overview.md).
65
+
The way partitions are managed is that each partition is assigned a roughly equal slice out of the range of partition key values. For this reason, you should choose a partition key that's relatively random or evenly distributed. Otherwise, you get *hot partitions* and *cold partitions*, which see substantially more or fewer requests. For information on avoiding this condition, see [Partitioning and horizontal scaling in Azure Cosmos DB](../partitioning-overview.md).
66
66
67
67
## Create a database account
68
68
@@ -85,7 +85,7 @@ Before you can create a document database, you need to create an API for NoSQL a
85
85
86
86
## Clone the sample application
87
87
88
-
Now let's switch to working with code. Let's clone an API for NoSQL app from GitHub, set the connection string, and run it. You see how easy it is to work with data programmatically.
88
+
Now let's switch to working with code. Let's clone an API for NoSQL app from GitHub, set the connection string, and run it.
89
89
90
90
Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.
91
91
@@ -99,17 +99,17 @@ This step is optional. If you're interested in learning how the database resourc
In this section, neither the configurations nor the code has any authentication operations. However, connecting to Azure service requires authentication. To complete the authentication, you need to use Azure Identity. Spring Cloud Azure uses `DefaultAzureCredential`, which is provided by Azure Identity to help you get credentials without any code changes.
102
+
In this section, the configurations and the code don't have any authentication operations. However, connecting to Azure service requires authentication. To complete the authentication, you need to use Azure Identity. Spring Cloud Azure uses `DefaultAzureCredential`, which Azure Identity provides to help you get credentials without any code changes.
103
103
104
-
`DefaultAzureCredential` supports multiple authentication methods and determines which method should be used at runtime. This approach enables your app to use different authentication methods in different environments (local vs. production) without implementing environment-specific code. For more information, see the [Default Azure credential](/azure/developer/java/sdk/identity-azure-hosted-auth#default-azure-credential) section of [Authenticate Azure-hosted Java applications](/azure/developer/java/sdk/identity-azure-hosted-auth).
104
+
`DefaultAzureCredential` supports multiple authentication methods and determines which method to use at runtime. This approach enables your app to use different authentication methods in different environments (local vs. production) without implementing environment-specific code. For more information, see the [Default Azure credential](/azure/developer/java/sdk/identity-azure-hosted-auth#default-azure-credential) section of [Authenticate Azure-hosted Java applications](/azure/developer/java/sdk/identity-azure-hosted-auth).
You can authenticate to Cosmos DB for NoSQL using `DefaultAzureCredential` by adding the `azure-identity`[dependency](https://mvnrepository.com/artifact/com.azure/azure-identity) to your application. `DefaultAzureCredential`will automatically discover and use the account you signed in with in the previous step.
112
+
You can authenticate to Cosmos DB for NoSQL using `DefaultAzureCredential` by adding the `azure-identity`[dependency](https://mvnrepository.com/artifact/com.azure/azure-identity) to your application. `DefaultAzureCredential` automatically discovers and uses the account you signed in with in the previous step.
113
113
114
114
### Application configuration file
115
115
@@ -124,41 +124,7 @@ spring:
124
124
database: ${COSMOS_DATABASE}
125
125
```
126
126
127
-
After creating the Azure Cosmos DB account, database and container, Spring Boot/Spring Data will connect to the database and container for `delete`, `add`, and `find` operations.
128
-
129
-
### Java source
130
-
131
-
Spring Data provides a simple, clean, standardized, and platform-independent interface for operating on datastores, as shown in the following examples. These CRUD and query examples enable you to manipulate Azure Cosmos DB documents by using Spring Data Azure Cosmos DB. These examples build on the Spring Data GitHub sample linked to earlier in this article.
132
-
133
-
* Item creation and updates by using the `save` method.
134
-
135
-
```java
136
-
// Save the User class to Azure Cosmos DB database.
137
-
final Mono<User> saveUserMono = repository.save(testUser);
138
-
```
139
-
140
-
* Point-reads using the derived query method defined in the repository. The `findById` performs point-reads for `repository`. The fields mentioned in the method name cause Spring Data to execute a point-read defined by the `id` field:
141
-
142
-
```java
143
-
// Nothing happens until we subscribe to these Monos.
144
-
// findById will not return the user as user is not present.
145
-
final Mono<User> findByIdMono = repository.findById(testUser.getId());
146
-
final User findByIdUser = findByIdMono.block();
147
-
Assert.isNull(findByIdUser, "User must be null");
148
-
```
149
-
150
-
* Item deletes using `deleteAll`:
151
-
152
-
```java
153
-
repository.deleteAll().block();
154
-
LOGGER.info("Deleted all data in container.");
155
-
```
156
-
157
-
* Derived query based on repository method name. Spring Data implements the `repository` `findByFirstName` method as a Java SDK SQL query on the `firstName` field. This query couldn't be implemented as a point-read.
158
-
159
-
```java
160
-
final Flux<User> firstNameUserFlux = repository.findByFirstName("testFirstName");
161
-
```
127
+
After Spring Boot and Spring Data create the Azure Cosmos DB account, database, and container, they connect to the database and container for `delete`, `add`, and `find` operations.
162
128
163
129
### [Password](#tab/password)
164
130
@@ -176,11 +142,13 @@ spring:
176
142
database: ${COSMOS_DATABASE}
177
143
```
178
144
179
-
Once you create an Azure Cosmos DB account, database, and container, just fill-in-the-blanks in the config file and Spring Boot/Spring Data will automatically do the following: (1) create an underlying Java SDK `CosmosClient` instance with the URI and key, and (2) connect to the database and container. You're all set - no more resource management code!
145
+
Once you create an Azure Cosmos DB account, database, and container, just fill-in-the-blanks in the config file and Spring Boot/Spring Data does the following: (1) creates an underlying Java SDK `CosmosClient` instance with the URI and key, and (2) connects to the database and container. You're all set - no more resource management code!
146
+
147
+
---
180
148
181
149
### Java source
182
150
183
-
The Spring Data value-add also comes from its simple, clean, standardized and platform-independent interface for operating on datastores. Building on the Spring Data GitHub sample linked above, below are CRUD and query samples for manipulating Azure Cosmos DB documents with Spring Data Azure Cosmos DB.
151
+
Spring Data provides a simple, clean, standardized, and platform-independent interface for operating on datastores, as shown in the following examples. These CRUD and query examples enable you to manipulate Azure Cosmos DB documents by using Spring Data Azure Cosmos DB. These examples build on the Spring Data GitHub sample linked to earlier in this article.
184
152
185
153
* Item creation and updates by using the `save` method.
186
154
@@ -206,38 +174,36 @@ The Spring Data value-add also comes from its simple, clean, standardized and pl
206
174
LOGGER.info("Deleted all data in container.");
207
175
```
208
176
209
-
* Derived query based on repository method name. Spring Data implements the `repository` `findByFirstName` method as a Java SDK SQL query on the `firstName` field. This query couldn't be implemented as a point-read.
177
+
* Derived query based on repository method name. Spring Data implements the `repository` `findByFirstName` method as a Java SDK SQL query on the `firstName` field. You can't implement this query as a point-read.
210
178
211
179
```java
212
180
final Flux<User> firstNameUserFlux = repository.findByFirstName("testFirstName");
213
181
```
214
182
215
-
---
216
-
217
183
## Run the app
218
184
219
-
Now go back to the Azure portal to get your connection string information and launch the app with your endpoint information. This enables your app to communicate with your hosted database.
185
+
Now go back to the Azure portal to get your connection string information. Then, use the following steps to launch the app with your endpoint information so your app can communicate with your hosted database.
220
186
221
-
1. In the git terminal window, `cd` to the sample code folder.
187
+
1. In the Git terminal window, `cd` to the sample code folder.
222
188
223
189
```bash
224
190
cd azure-spring-boot-samples/cosmos/spring-cloud-azure-starter-data-cosmos/spring-cloud-azure-data-cosmos-sample
225
191
```
226
192
227
-
1. In the git terminal window, use the following command to install the required Spring Data Azure Cosmos DB packages.
193
+
1. In the Git terminal window, use the following command to install the required Spring Data Azure Cosmos DB packages.
228
194
229
195
```bash
230
196
mvn clean package
231
197
```
232
198
233
-
1. In the git terminal window, use the following command to start the Spring Data Azure Cosmos DB application:
199
+
1. In the Git terminal window, use the following command to start the Spring Data Azure Cosmos DB application:
234
200
235
201
```bash
236
202
mvn spring-boot:run
237
203
```
238
204
239
205
1. The app loads *application.yml* and connects the resources in your Azure Cosmos DB account.
240
-
1. The app performs point CRUD operations described above.
206
+
1. The app performs point CRUD operations described previously.
241
207
1. The app performs a derived query.
242
208
1. The app doesn't delete your resources. Switch back to the portal to [clean up the resources](#clean-up-resources) from your account if you want to avoid incurring charges.
243
209
@@ -251,7 +217,7 @@ Now go back to the Azure portal to get your connection string information and la
251
217
252
218
## Next steps
253
219
254
-
In this quickstart, you've learned how to create an Azure Cosmos DB for NoSQL account, create a document database and container using the Data Explorer, and run a Spring Data app to do the same thing programmatically. You can now import more data into your Azure Cosmos DB account.
220
+
In this quickstart, you learned how to create an Azure Cosmos DB for NoSQL account and create a document database and container using the Data Explorer. You then ran a Spring Data app to do the same thing programmatically. You can now import more data into your Azure Cosmos DB account.
255
221
256
222
Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
0 commit comments