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
@@ -74,7 +74,7 @@ Before you can create a document database, you need to create a API for NoSQL ac
74
74
75
75
## Clone the sample application
76
76
77
-
Now let's switch to working with code. Let's clone a API for NoSQL app from GitHub, set the connection string, and run it. You'll see how easy it is to work with data programmatically.
77
+
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'll see how easy it is to work with data programmatically.
78
78
79
79
Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.
80
80
@@ -116,6 +116,36 @@ This step is optional. If you're interested in learning how the database resourc
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.
122
+
123
+
1. In the git terminal window, `cd` to the sample code folder.
124
+
125
+
```bash
126
+
cd azure-cosmos-java-getting-started
127
+
```
128
+
129
+
2. In the git terminal window, use the following command to install the required Java packages.
130
+
131
+
```bash
132
+
mvn package
133
+
```
134
+
135
+
3. In the git terminal window, use the following command to start the Java application (replace SYNCASYNCMODE with `sync` or `async` depending on which sample code you would like to run, replace YOUR_COSMOS_DB_HOSTNAME with the quoted URI value from the portal, and replace YOUR_COSMOS_DB_MASTER_KEY with the quoted primary key from portal)
The terminal window displays a notification that the FamilyDB database was created.
142
+
143
+
4. The app creates database with name `AzureSampleFamilyDB`
144
+
5. The app creates container with name `FamilyContainer`
145
+
6. The app will perform point reads using object IDs and partition key value (which is lastName in our sample).
146
+
7. The app will query items to retrieve all families with last name in ('Andersen', 'Wakefield', 'Johnson')
147
+
8. The app doesn't delete the created resources. Return to the Azure portal to [clean up the resources](#clean-up-resources) from your account so you don't incur charges.
148
+
119
149
# [Async API](#tab/async)
120
150
121
151
### Managing database resources using the asynchronous (async) API
@@ -146,8 +176,6 @@ This step is optional. If you're interested in learning how the database resourc
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.
@@ -178,8 +206,177 @@ Now go back to the Azure portal to get your connection string information and la
178
206
6. The app will perform point reads using object IDs and partition key value (which is lastName in our sample).
179
207
7. The app will query items to retrieve all families with last name in ('Andersen', 'Wakefield', 'Johnson')
180
208
209
+
8. The app doesn't delete the created resources. Return to the Azure portal to [clean up the resources](#clean-up-resources) from your account so you don't incur charges.
You can authenticate to Cosmos DB forNoSQL 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 within the previous step.
224
+
225
+
### Managing database resources using the synchronous (sync) API
226
+
227
+
*`CosmosClient` initialization. The `CosmosClient` provides client-side logical representation for the Azure Cosmos DB database service. This client is used to configure and execute requests against the service.
* Use the [`az cosmosdb sql database create`](/cli/azure/cosmosdb/sql/database#az-cosmosdb-sql-database-create) and [`az cosmosdb sql container create`](/cli/azure/cosmosdb/sql/container#az-cosmosdb-sql-container-create) commands to create a Cosmos DB NoSQL database and container.
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.
266
+
267
+
1. In the git terminal window, `cd` to the sample code folder.
268
+
269
+
```bash
270
+
cd azure-cosmos-java-getting-started
271
+
```
272
+
273
+
2. In the git terminal window, use the following command to install the required Java packages.
274
+
275
+
```bash
276
+
mvn package
277
+
```
278
+
279
+
3. In the git terminal window, use the following command to start the Java application. Replace `SYNCASYNCMODE` with `sync-passwordless` or `async-passwordless`, depending upon which sample code you'd like to run. Replace `YOUR_COSMOS_DB_HOSTNAME` with the quoted URI value from the portal, and replace `YOUR_COSMOS_DB_MASTER_KEY` with the quoted primary key from portal.
The terminal window displays a notification that the FamilyDB database was created.
286
+
287
+
4. The app will reference the database and container you created via Azure CLI earlier.
288
+
289
+
5. The app will perform point reads using object IDs and partition key value (which is lastName in our sample).
290
+
6. The app will query items to retrieve all families with last name in ('Andersen', 'Wakefield', 'Johnson')
291
+
181
292
7. The app doesn't delete the created resources. Switch back to the portal to [clean up the resources](#clean-up-resources). from your account so that you don't incur charges.
You can authenticate to Cosmos DB forNoSQL 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 within the previous step.
305
+
306
+
### Managing database resources using the asynchronous (async) API
307
+
308
+
* Async API calls return immediately, without waiting for a response from the server. In light of this, the following code snippets show proper design patterns for accomplishing all of the preceding management tasks using async API.
309
+
310
+
*`CosmosAsyncClient` initialization. The `CosmosAsyncClient` provides client-side logical representation for the Azure Cosmos DB database service. This client is used to configure and execute asynchronous requests against the service.
* Use the [`az cosmosdb sql database create`](/cli/azure/cosmosdb/sql/database#az-cosmosdb-sql-database-create) and [`az cosmosdb sql container create`](/cli/azure/cosmosdb/sql/container#az-cosmosdb-sql-container-create) commands to create a Cosmos DB NoSQL database and container.
315
+
316
+
```azurecli-interactive
317
+
# Create a SQL API database
318
+
az cosmosdb sql database create \
319
+
--account-name msdocs-cosmos-nosql \
320
+
--resource-group msdocs \
321
+
--name AzureSampleFamilyDB
322
+
```
323
+
324
+
```azurecli-interactive
325
+
# Create a SQL API container
326
+
az cosmosdb sql container create \
327
+
--account-name msdocs-cosmos-nosql \
328
+
--resource-group msdocs \
329
+
--database-name AzureSampleFamilyDB \
330
+
--name FamilyContainer \
331
+
--partition-key-path '/lastName'
332
+
```
333
+
334
+
* As with the sync API, item creation is accomplished using the `createItem` method. This example shows how to efficiently issue numerous async `createItem` requests by subscribing to a Reactive Stream which issues the requests and prints notifications. Since this simple example runs to completion and terminates, `CountDownLatch` instances are used to ensure the program does not terminate during item creation. **The proper asynchronous programming practice is not to block on async calls - in realistic use-cases requests are generated from a main() loop that executes indefinitely, eliminating the need to latch on async calls.**
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.
349
+
350
+
1. In the git terminal window, `cd` to the sample code folder.
351
+
352
+
```bash
353
+
cd azure-cosmos-java-getting-started
354
+
```
355
+
356
+
2. In the git terminal window, use the following command to install the required Java packages.
357
+
358
+
```bash
359
+
mvn package
360
+
```
361
+
362
+
3. In the git terminal window, use the following command to start the Java application (replace SYNCASYNCMODE with `sync-passwordless` or `async-passwordless` depending on which sample code you would like to run, replace YOUR_COSMOS_DB_HOSTNAME with the quoted URI value from the portal, and replace YOUR_COSMOS_DB_MASTER_KEY with the quoted primary key from portal)
The terminal window displays a notification that the `AzureSampleFamilyDB` database was created.
369
+
370
+
4. The app will reference the database and container you created via Azure CLI earlier.
371
+
372
+
5. The app will perform point reads using object IDs and partition key value (which is lastName in our sample).
373
+
6. The app will query items to retrieve all families with last name in ('Andersen', 'Wakefield', 'Johnson')
374
+
375
+
7. The app doesn't delete the created resources. Switch back to the portal to [clean up the resources](#clean-up-resources). from your account so that you don't incur charges.
`DefaultAzureCredential` is a class provided by the Azure Identity library for Java. To learn more about `DefaultAzureCredential`, see the [Azure authentication with Java and Azure Identity](/azure/developer/java/sdk/identity). `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.
14
+
15
+
For example, your app can authenticate using your Visual Studio sign-in credentials when developing locally, and then use a [managed identity](../../articles/active-directory/managed-identities-azure-resources/overview.md) once it has been deployed to Azure. No code changes are required for this transition.
0 commit comments