Skip to content

Commit 3f73a3c

Browse files
Merge pull request #225358 from TheovanKraay/cosmos-java-passwordless
passwordless cosmos java changes
2 parents 0c6720d + e8e4dee commit 3f73a3c

File tree

2 files changed

+216
-4
lines changed

2 files changed

+216
-4
lines changed

articles/cosmos-db/nosql/quickstart-java.md

Lines changed: 201 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ As items are inserted into an Azure Cosmos DB container, the database grows hori
5555

5656
## Create a database account
5757

58-
Before you can create a document database, you need to create a API for NoSQL account with Azure Cosmos DB.
58+
Before you can create a document database, you need to create an API for NoSQL account with Azure Cosmos DB.
5959

6060
[!INCLUDE [cosmos-db-create-dbaccount](../includes/cosmos-db-create-dbaccount.md)]
6161

@@ -74,7 +74,7 @@ Before you can create a document database, you need to create a API for NoSQL ac
7474

7575
## Clone the sample application
7676

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.
7878

7979
Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.
8080

@@ -116,6 +116,36 @@ This step is optional. If you're interested in learning how the database resourc
116116

117117
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/sync/SyncMain.java?name=QueryItems)]
118118

119+
## Run the app
120+
121+
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)
136+
137+
```bash
138+
mvn exec:java@SYNCASYNCMODE -DACCOUNT_HOST=YOUR_COSMOS_DB_HOSTNAME -DACCOUNT_KEY=YOUR_COSMOS_DB_MASTER_KEY
139+
```
140+
141+
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+
119149
# [Async API](#tab/async)
120150

121151
### 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
146176

147177
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/async/AsyncMain.java?name=QueryItems)]
148178

149-
---
150-
151179
## Run the app
152180

153181
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
178206
6. The app will perform point reads using object IDs and partition key value (which is lastName in our sample).
179207
7. The app will query items to retrieve all families with last name in ('Andersen', 'Wakefield', 'Johnson')
180208

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.
210+
211+
[!INCLUDE [passwordless-overview](../../../includes/passwordless/passwordless-overview.md)]
212+
213+
## [Passwordless Sync API](#tab/passwordlesssync)
214+
215+
[!INCLUDE [java-default-azure-credential-overview](../../../includes/passwordless/java-default-azure-credential-overview.md)]
216+
217+
[!INCLUDE [cosmos-nosql-create-assign-roles](../../../includes/passwordless/cosmos-nosql/cosmos-nosql-create-assign-roles.md)]
218+
219+
## Authenticate using DefaultAzureCredential
220+
221+
[!INCLUDE [default-azure-credential-sign-in](../../../includes/passwordless/default-azure-credential-sign-in.md)]
222+
223+
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.
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.
228+
229+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/sync/SyncPasswordlessMain.java?name=CreatePasswordlessSyncClient)]
230+
231+
* 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.
232+
233+
```azurecli-interactive
234+
# Create a SQL API database
235+
az cosmosdb sql database create \
236+
--account-name msdocs-cosmos-nosql \
237+
--resource-group msdocs \
238+
--name AzureSampleFamilyDB
239+
```
240+
241+
```azurecli-interactive
242+
# Create a SQL API container
243+
az cosmosdb sql container create \
244+
--account-name msdocs-cosmos-nosql \
245+
--resource-group msdocs \
246+
--database-name AzureSampleFamilyDB \
247+
--name FamilyContainer \
248+
--partition-key-path '/lastName'
249+
```
250+
251+
* Item creation by using the `createItem` method.
252+
253+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/sync/SyncPasswordlessMain.java?name=CreateItem)]
254+
255+
* Point reads are performed using `readItem` method.
256+
257+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/sync/SyncPasswordlessMain.java?name=ReadItem)]
258+
259+
* SQL queries over JSON are performed using the `queryItems` method.
260+
261+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/sync/SyncPasswordlessMain.java?name=QueryItems)]
262+
263+
## Run the app
264+
265+
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.
280+
281+
```bash
282+
mvn exec:java@SYNCASYNCMODE -DACCOUNT_HOST=YOUR_COSMOS_DB_HOSTNAME -DACCOUNT_KEY=YOUR_COSMOS_DB_MASTER_KEY
283+
```
284+
285+
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+
181292
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.
182293

294+
## [Passwordless Aync API](#tab/passwordlessasync)
295+
296+
[!INCLUDE [java-default-azure-credential-overview](../../../includes/passwordless/java-default-azure-credential-overview.md)]
297+
298+
[!INCLUDE [cosmos-nosql-create-assign-roles](../../../includes/passwordless/cosmos-nosql/cosmos-nosql-create-assign-roles.md)]
299+
300+
## Authenticate using DefaultAzureCredential
301+
302+
[!INCLUDE [default-azure-credential-sign-in](../../../includes/passwordless/default-azure-credential-sign-in.md)]
303+
304+
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.
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.
311+
312+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/async/AsyncPasswordlessMain.java?name=CreatePasswordlessAsyncClient)]
313+
314+
* 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.**
335+
336+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/async/AsyncPasswordlessMain.java?name=CreateItem)]
337+
338+
* As with the sync API, point reads are performed using `readItem` method.
339+
340+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/async/AsyncPasswordlessMain.java?name=ReadItem)]
341+
342+
* As with the sync API, SQL queries over JSON are performed using the `queryItems` method.
343+
344+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/async/AsyncPasswordlessMain.java?name=QueryItems)]
345+
346+
## Run the app
347+
348+
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)
363+
364+
```bash
365+
mvn exec:java@SYNCASYNCMODE -DACCOUNT_HOST=YOUR_COSMOS_DB_HOSTNAME -DACCOUNT_KEY=YOUR_COSMOS_DB_MASTER_KEY
366+
```
367+
368+
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.
376+
377+
---
378+
379+
183380
## Review SLAs in the Azure portal
184381

185382
[!INCLUDE [cosmosdb-tutorial-review-slas](../includes/cosmos-db-tutorial-review-slas.md)]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: "include file"
3+
description: "include file"
4+
services: storage
5+
author: TheovanKraay
6+
ms.service: storage
7+
ms.topic: include
8+
ms.date: 01/27/2023
9+
ms.author: thvankra
10+
ms.custom: include file
11+
---
12+
13+
`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

Comments
 (0)