Skip to content
This repository was archived by the owner on Feb 15, 2024. It is now read-only.

Commit 6243483

Browse files
authored
Merge pull request #1 from Melanie-Ma/master
Change cosoms sample from track1 to track2(Azure CosmosDB sample forhigh availability)
2 parents 7bc01d6 + fc3e3ee commit 6243483

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@
6161
<version>3.3</version>
6262
</dependency>
6363
<dependency>
64-
<groupId>com.microsoft.azure</groupId>
65-
<artifactId>azure-documentdb</artifactId>
66-
<version>1.9.4</version>
64+
<groupId>com.azure</groupId>
65+
<artifactId>azure-cosmos</artifactId>
66+
<version>4.4.0</version>
6767
</dependency>
6868
</dependencies>
6969
</project>

src/main/java/com/microsoft/azure/management/cosmosdb/samples/CreateCosmosDBWithEventualConsistency.java

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66

77
package com.microsoft.azure.management.cosmosdb.samples;
88

9+
import com.azure.cosmos.ConsistencyLevel;
10+
import com.azure.cosmos.CosmosClient;
11+
import com.azure.cosmos.CosmosClientBuilder;
12+
import com.azure.cosmos.CosmosDatabase;
13+
import com.azure.cosmos.CosmosException;
14+
import com.azure.cosmos.models.CosmosContainerProperties;
15+
import com.azure.cosmos.models.ThroughputProperties;
916
import com.microsoft.azure.CloudException;
1017
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
1118
import com.microsoft.azure.management.Azure;
12-
import com.microsoft.azure.documentdb.DocumentClient;
13-
import com.microsoft.azure.documentdb.ConsistencyLevel;
14-
import com.microsoft.azure.documentdb.ConnectionPolicy;
15-
import com.microsoft.azure.documentdb.Database;
16-
import com.microsoft.azure.documentdb.DocumentCollection;
17-
import com.microsoft.azure.documentdb.DocumentClientException;
18-
import com.microsoft.azure.documentdb.RequestOptions;
1919
import com.microsoft.azure.management.cosmosdb.CosmosDBAccount;
2020
import com.microsoft.azure.management.cosmosdb.DatabaseAccountKind;
2121
import com.microsoft.azure.management.cosmosdb.DatabaseAccountListKeysResult;
@@ -30,12 +30,12 @@
3030
* Azure CosmosDB sample for high availability.
3131
* - Create a CosmosDB configured with eventual consistency
3232
* - Get the credentials for the CosmosDB
33-
* - add collection to the CosmosDB
33+
* - add container to the CosmosDB
3434
* - Delete the CosmosDB.
3535
*/
3636
public final class CreateCosmosDBWithEventualConsistency {
3737
static final String DATABASE_ID = "TestDB";
38-
static final String COLLECTION_ID = "TestCollection";
38+
static final String CONTAINER_ID = "TestContainer";
3939

4040
/**
4141
* Main function which runs the actual sample.
@@ -73,10 +73,10 @@ public static boolean runSample(Azure azure, String clientId) {
7373
String endPoint = cosmosDBAccount.documentEndpoint();
7474

7575
//============================================================
76-
// Connect to CosmosDB and add a collection
76+
// Connect to CosmosDB and add a container
7777

78-
System.out.println("Connecting and adding collection");
79-
createDBAndAddCollection(masterKey, endPoint);
78+
System.out.println("Connecting and adding container");
79+
createDBAndAddContainer(masterKey, endPoint);
8080

8181
//============================================================
8282
// Delete CosmosDB
@@ -106,34 +106,29 @@ public static boolean runSample(Azure azure, String clientId) {
106106
return false;
107107
}
108108

109-
private static void createDBAndAddCollection(String masterKey, String endPoint) throws DocumentClientException {
109+
private static void createDBAndAddContainer(String masterKey, String endPoint) throws CosmosException {
110110
try {
111-
DocumentClient documentClient = new DocumentClient(endPoint,
112-
masterKey, ConnectionPolicy.GetDefault(),
113-
ConsistencyLevel.Session);
111+
CosmosClient cosmosClient = new CosmosClientBuilder()
112+
.endpoint(endPoint)
113+
.key(masterKey)
114+
.consistencyLevel(ConsistencyLevel.SESSION)
115+
.buildClient();
114116

115-
// Define a new database using the id above.
116-
Database myDatabase = new Database();
117-
myDatabase.setId(DATABASE_ID);
118117

119-
myDatabase = documentClient.createDatabase(myDatabase, null)
120-
.getResource();
118+
cosmosClient.createDatabase(DATABASE_ID);
121119

122-
System.out.println("Created a new database:");
123-
System.out.println(myDatabase.toString());
120+
CosmosDatabase cosmosDatabase = cosmosClient.getDatabase(DATABASE_ID);
124121

125-
// Define a new collection using the id above.
126-
DocumentCollection myCollection = new DocumentCollection();
127-
myCollection.setId(COLLECTION_ID);
122+
System.out.println("Created a new database:");
123+
System.out.println(cosmosDatabase.getId());
128124

129-
// Set the provisioned throughput for this collection to be 1000 RUs.
130-
RequestOptions requestOptions = new RequestOptions();
131-
requestOptions.setOfferThroughput(1000);
125+
// Set the provisioned throughput for this container to be 1000 RUs.
126+
Integer throughput = 1000;
127+
ThroughputProperties properties = ThroughputProperties.createManualThroughput(throughput);
132128

133-
// Create a new collection.
134-
myCollection = documentClient.createCollection(
135-
"dbs/" + DATABASE_ID, myCollection, requestOptions)
136-
.getResource();
129+
// Create a new container.
130+
CosmosContainerProperties containerProperties = new CosmosContainerProperties(CONTAINER_ID, "/id");
131+
cosmosDatabase.createContainerIfNotExists(containerProperties, properties);
137132
} catch (Exception ex) {
138133
throw ex;
139134
}

0 commit comments

Comments
 (0)