|
6 | 6 |
|
7 | 7 | package com.microsoft.azure.management.cosmosdb.samples;
|
8 | 8 |
|
| 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; |
9 | 16 | import com.microsoft.azure.CloudException;
|
10 | 17 | import com.microsoft.azure.credentials.ApplicationTokenCredentials;
|
11 | 18 | 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; |
19 | 19 | import com.microsoft.azure.management.cosmosdb.CosmosDBAccount;
|
20 | 20 | import com.microsoft.azure.management.cosmosdb.DatabaseAccountKind;
|
21 | 21 | import com.microsoft.azure.management.cosmosdb.DatabaseAccountListKeysResult;
|
|
30 | 30 | * Azure CosmosDB sample for high availability.
|
31 | 31 | * - Create a CosmosDB configured with eventual consistency
|
32 | 32 | * - Get the credentials for the CosmosDB
|
33 |
| - * - add collection to the CosmosDB |
| 33 | + * - add container to the CosmosDB |
34 | 34 | * - Delete the CosmosDB.
|
35 | 35 | */
|
36 | 36 | public final class CreateCosmosDBWithEventualConsistency {
|
37 | 37 | static final String DATABASE_ID = "TestDB";
|
38 |
| - static final String COLLECTION_ID = "TestCollection"; |
| 38 | + static final String CONTAINER_ID = "TestContainer"; |
39 | 39 |
|
40 | 40 | /**
|
41 | 41 | * Main function which runs the actual sample.
|
@@ -73,10 +73,10 @@ public static boolean runSample(Azure azure, String clientId) {
|
73 | 73 | String endPoint = cosmosDBAccount.documentEndpoint();
|
74 | 74 |
|
75 | 75 | //============================================================
|
76 |
| - // Connect to CosmosDB and add a collection |
| 76 | + // Connect to CosmosDB and add a container |
77 | 77 |
|
78 |
| - System.out.println("Connecting and adding collection"); |
79 |
| - createDBAndAddCollection(masterKey, endPoint); |
| 78 | + System.out.println("Connecting and adding container"); |
| 79 | + createDBAndAddContainer(masterKey, endPoint); |
80 | 80 |
|
81 | 81 | //============================================================
|
82 | 82 | // Delete CosmosDB
|
@@ -106,34 +106,29 @@ public static boolean runSample(Azure azure, String clientId) {
|
106 | 106 | return false;
|
107 | 107 | }
|
108 | 108 |
|
109 |
| - private static void createDBAndAddCollection(String masterKey, String endPoint) throws DocumentClientException { |
| 109 | + private static void createDBAndAddContainer(String masterKey, String endPoint) throws CosmosException { |
110 | 110 | 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(); |
114 | 116 |
|
115 |
| - // Define a new database using the id above. |
116 |
| - Database myDatabase = new Database(); |
117 |
| - myDatabase.setId(DATABASE_ID); |
118 | 117 |
|
119 |
| - myDatabase = documentClient.createDatabase(myDatabase, null) |
120 |
| - .getResource(); |
| 118 | + cosmosClient.createDatabase(DATABASE_ID); |
121 | 119 |
|
122 |
| - System.out.println("Created a new database:"); |
123 |
| - System.out.println(myDatabase.toString()); |
| 120 | + CosmosDatabase cosmosDatabase = cosmosClient.getDatabase(DATABASE_ID); |
124 | 121 |
|
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()); |
128 | 124 |
|
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); |
132 | 128 |
|
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); |
137 | 132 | } catch (Exception ex) {
|
138 | 133 | throw ex;
|
139 | 134 | }
|
|
0 commit comments