Skip to content

Commit 70220ab

Browse files
authored
Merge pull request #104790 from anfeldma-ms/QuickstartSyncAsync
Quickstart updated to SDK v4 sync & async
2 parents 9169542 + c2d9fc7 commit 70220ab

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

articles/cosmos-db/create-sql-api-java.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,60 @@ git clone https://github.com/Azure-Samples/azure-cosmos-java-getting-started.git
6464
This step is optional. If you're interested in learning how the database resources are created in the code, you can review the following snippets. Otherwise, you can skip ahead to [Run the app
6565
](#run-the-app).
6666

67+
### Managing database resources using the synchronous (sync) API
68+
6769
* `CosmosClient` initialization. The `CosmosClient` provides client-side logical representation for the Azure Cosmos database service. This client is used to configure and execute requests against the service.
6870

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

71-
* CosmosDatabase creation.
73+
* `CosmosDatabase` creation.
7274

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

75-
* CosmosContainer creation.
77+
* `CosmosContainer` creation.
7678

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

7981
* Item creation by using the `createItem` method.
8082

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

83-
* Point reads are performed using `getItem` and `read` method
85+
* Point reads are performed using `readItem` method.
8486

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

8789
* SQL queries over JSON are performed using the `queryItems` method.
8890

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

93+
### Managing database resources using the asynchronous (async) API
94+
95+
* 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.
96+
97+
* `CosmosAsyncClient` initialization. The `CosmosAsyncClient` provides client-side logical representation for the Azure Cosmos database service. This client is used to configure and execute asynchronous requests against the service.
98+
99+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/async/AsyncMain.java?name=CreateAsyncClient)]
100+
101+
* `CosmosAsyncDatabase` creation.
102+
103+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/async/AsyncMain.java?name=CreateDatabaseIfNotExists)]
104+
105+
* `CosmosAsyncContainer` creation.
106+
107+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/async/AsyncMain.java?name=CreateContainerIfNotExists)]
108+
109+
* 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.**
110+
111+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/async/AsyncMain.java?name=CreateItem)]
112+
113+
* As with the sync API, point reads are performed using `readItem` method.
114+
115+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/async/AsyncMain.java?name=ReadItem)]
116+
117+
* As with the sync API, SQL queries over JSON are performed using the `queryItems` method.
118+
119+
[!code-java[](~/azure-cosmosdb-java-v4-getting-started/src/main/java/com/azure/cosmos/sample/async/AsyncMain.java?name=QueryItems)]
120+
91121
## Run the app
92122

93123
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.
@@ -104,10 +134,10 @@ Now go back to the Azure portal to get your connection string information and la
104134
mvn package
105135
```
106136

107-
3. In the git terminal window, use the following command to start the Java application (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)
137+
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)
108138

109139
```bash
110-
mvn exec:java -DACCOUNT_HOST=YOUR_COSMOS_DB_HOSTNAME -DACCOUNT_KEY=YOUR_COSMOS_DB_MASTER_KEY
140+
mvn exec:java@SYNCASYNCMODE -DACCOUNT_HOST=YOUR_COSMOS_DB_HOSTNAME -DACCOUNT_KEY=YOUR_COSMOS_DB_MASTER_KEY
111141
112142
```
113143

0 commit comments

Comments
 (0)