Skip to content
This repository was archived by the owner on Jul 13, 2026. It is now read-only.

Commit 784d2aa

Browse files
author
Theo van Kraay
committed
tidy up
1 parent 1293885 commit 784d2aa

4 files changed

Lines changed: 70 additions & 191 deletions

File tree

src/main/java/com/azure/cosmos/examples/analyticalcontainercrud/async/AnalyticalContainerCRUDQuickstartAsync.java

Lines changed: 24 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
import com.azure.cosmos.models.CosmosContainerResponse;
1515
import com.azure.cosmos.models.CosmosDatabaseRequestOptions;
1616
import com.azure.cosmos.models.CosmosDatabaseResponse;
17-
import com.azure.cosmos.models.ThroughputProperties;
18-
import com.azure.cosmos.util.CosmosPagedFlux;
19-
20-
import reactor.core.publisher.Flux;
21-
import reactor.core.publisher.Mono;
22-
2317
import org.slf4j.Logger;
2418
import org.slf4j.LoggerFactory;
2519

@@ -31,7 +25,6 @@ public class AnalyticalContainerCRUDQuickstartAsync {
3125
private final String containerName = "FamilyContainer";
3226

3327
private CosmosAsyncDatabase database;
34-
private CosmosAsyncContainer container;
3528

3629
protected static Logger logger = LoggerFactory.getLogger(AnalyticalContainerCRUDQuickstartAsync.class);
3730

@@ -40,8 +33,7 @@ public void close() {
4033
}
4134

4235
/**
43-
* Sample to demonstrate the following ANALYTICAL STORE container CRUD
44-
* operations:
36+
* Sample to demonstrate the following ANALYTICAL STORE container CRUD operations:
4537
* -Create
4638
* -Update throughput
4739
* -Read by ID
@@ -57,7 +49,7 @@ public static void main(String[] args) {
5749
logger.info("Demo complete, please hold while resources are released");
5850
} catch (Exception e) {
5951
e.printStackTrace();
60-
logger.error("Cosmos getStarted failed", e);
52+
logger.error(String.format("Cosmos getStarted failed with %s", e));
6153
} finally {
6254
logger.info("Closing the client");
6355
p.shutdown();
@@ -68,20 +60,17 @@ private void containerCRUDDemo() throws Exception {
6860

6961
logger.info("Using Azure Cosmos DB endpoint: {}", AccountSettings.HOST);
7062

71-
// Create async client
63+
// Create async client
7264
client = new CosmosClientBuilder()
7365
.endpoint(AccountSettings.HOST)
7466
.key(AccountSettings.MASTER_KEY)
7567
.consistencyLevel(ConsistencyLevel.EVENTUAL)
7668
.contentResponseOnWriteEnabled(true)
7769
.buildAsyncClient();
7870

71+
7972
createDatabaseIfNotExists();
8073
createContainerIfNotExists();
81-
updateContainerThroughput();
82-
83-
readContainerById();
84-
readAllContainers();
8574

8675
// deleteAContainer() is called at shutdown()
8776

@@ -91,9 +80,7 @@ private void containerCRUDDemo() throws Exception {
9180
private void createDatabaseIfNotExists() throws Exception {
9281
logger.info("Create database {} if not exists...", databaseName);
9382

94-
// Create database if not exists - this is async but we block to make sure
95-
// database and containers are created before sample runs the CRUD operations on
96-
// them
83+
// Create database if not exists
9784
CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists(databaseName).block();
9885
database = client.getDatabase(databaseResponse.getProperties().getId());
9986

@@ -102,109 +89,52 @@ private void createDatabaseIfNotExists() throws Exception {
10289

10390
// Container create
10491
private void createContainerIfNotExists() throws Exception {
105-
logger.info("Create container " + containerName + " if not exists.");
92+
logger.info("Create container {} if not exists.", containerName);
10693

107-
// Create container if not exists
108-
CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/lastName");
94+
// Create container if not exists
95+
CosmosContainerProperties containerProperties =
96+
new CosmosContainerProperties(containerName, "/lastName");
10997

11098
// Set analytical store properties
11199
containerProperties.setAnalyticalStoreTimeToLiveInSeconds(-1);
112100

113-
// Create container - this is async but we block to make sure
114-
// database and containers are created before sample runs the CRUD operations on
115-
// them
116-
CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties).block();
117-
CosmosAsyncContainer container = database.getContainer(containerResponse.getProperties().getId());
118-
119-
logger.info("Done.");
120-
}
121-
122-
// Update container throughput
123-
private void updateContainerThroughput() throws Exception {
124-
logger.info("Update throughput for container " + containerName + ".");
125-
126-
// Specify new throughput value
127-
ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(800);
128-
container.replaceThroughput(throughputProperties).block();
129-
130-
logger.info("Done.");
131-
}
132-
133-
// Container read
134-
private void readContainerById() throws Exception {
135-
logger.info("Read container " + containerName + " by ID.");
136-
137-
// Get container id
138-
container = database.getContainer(containerName);
139-
140-
logger.info("container id: "+container.getId());
141-
}
101+
// Create container
102+
CosmosContainerResponse databaseResponse = database.createContainerIfNotExists(containerProperties).block();
103+
CosmosAsyncContainer container = database.getContainer(databaseResponse.getProperties().getId());
142104

143-
// Container read all
144-
private void readAllContainers() throws Exception {
145-
logger.info("Read all containers in database " + databaseName + ".");
146-
147-
// Read all containers in the account
148-
CosmosPagedFlux<CosmosContainerProperties> containers = database.readAllContainers();
149-
150-
// Print
151-
String msg = "Listing containers in database:\n";
152-
containers.byPage(100).flatMap(readAllContainersResponse -> {
153-
logger.info("read " +
154-
readAllContainersResponse.getResults().size() + " containers(s)"
155-
+ " with request charge of " + readAllContainersResponse.getRequestCharge());
156-
157-
for (CosmosContainerProperties response : readAllContainersResponse.getResults()) {
158-
logger.info("container id: " + response.getId());
159-
// Got a page of query result with
160-
}
161-
return Flux.empty();
162-
}).blockLast();
163-
// for(CosmosContainerProperties containerProps : containers) {
164-
// msg += String.format("-Container ID: %s\n",containerProps.getId());
165-
// }
166-
logger.info(msg + "\n");
167105
logger.info("Done.");
168106
}
169107

170108
// Container delete
171109
private void deleteAContainer() throws Exception {
172-
logger.info("Delete container " + containerName + " by ID.");
110+
logger.info("Delete container {} by ID.", containerName);
173111

174112
// Delete container
175-
CosmosContainerResponse containerResp = database.getContainer(containerName)
176-
.delete()
177-
.doOnError(throwable -> {
178-
logger.warn("Delete container {} failed ", containerName, throwable);
179-
}).doOnSuccess(response -> {
180-
logger.info("Delete container {} succeeded: {}", containerName);
181-
}).block();
113+
CosmosContainerResponse containerResp = database.getContainer(containerName).delete(new CosmosContainerRequestOptions()).block();
114+
logger.info("Status code for container delete: {}",containerResp.getStatusCode());
115+
116+
logger.info("Done.");
182117
}
183118

184119
// Database delete
185120
private void deleteADatabase() throws Exception {
186-
logger.info("Last step: delete database " + databaseName + " by ID.");
121+
logger.info("Last step: delete database {} by ID.", databaseName);
187122

188123
// Delete database
189-
CosmosDatabaseResponse dbResp = client.getDatabase(databaseName)
190-
.delete(new CosmosDatabaseRequestOptions())
191-
.doOnError(throwable -> {
192-
logger.warn("Delete database {} failed ", containerName, throwable);
193-
}).doOnSuccess(response -> {
194-
logger.info("Delete database {} succeeded: {}", containerName);
195-
}).block();
196-
logger.info("Status code for database delete: {}", dbResp.getStatusCode());
124+
CosmosDatabaseResponse dbResp = client.getDatabase(databaseName).delete(new CosmosDatabaseRequestOptions()).block();
125+
logger.info("Status code for database delete: {}",dbResp.getStatusCode());
126+
127+
logger.info("Done.");
197128
}
198129

199130
// Cleanup before close
200131
private void shutdown() {
201132
try {
202-
// Clean shutdown
133+
//Clean shutdown
203134
deleteAContainer();
204135
deleteADatabase();
205136
} catch (Exception err) {
206-
logger.error(
207-
"Deleting Cosmos DB resources failed, will still attempt to close the client. See stack trace below.");
137+
logger.error("Deleting Cosmos DB resources failed, will still attempt to close the client. See stack trace below.");
208138
err.printStackTrace();
209139
}
210140
client.close();

src/main/java/com/azure/cosmos/examples/analyticalcontainercrud/sync/AnalyticalContainerCRUDQuickstart.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static void main(String[] args) {
5858

5959
private void containerCRUDDemo() throws Exception {
6060

61-
logger.info("Using Azure Cosmos DB endpoint: " + AccountSettings.HOST);
61+
logger.info("Using Azure Cosmos DB endpoint: {}", AccountSettings.HOST);
6262

6363
// Create sync client
6464
client = new CosmosClientBuilder()
@@ -78,7 +78,7 @@ private void containerCRUDDemo() throws Exception {
7878

7979
// Database Create
8080
private void createDatabaseIfNotExists() throws Exception {
81-
logger.info("Create database " + databaseName + " if not exists...");
81+
logger.info("Create database {} if not exists...", databaseName);
8282

8383
// Create database if not exists
8484
CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists(databaseName);
@@ -89,7 +89,7 @@ private void createDatabaseIfNotExists() throws Exception {
8989

9090
// Container create
9191
private void createContainerIfNotExists() throws Exception {
92-
logger.info("Create container " + containerName + " if not exists.");
92+
logger.info("Create container {} if not exists.", containerName);
9393

9494
// Create container if not exists
9595
CosmosContainerProperties containerProperties =
@@ -107,7 +107,7 @@ private void createContainerIfNotExists() throws Exception {
107107

108108
// Container delete
109109
private void deleteAContainer() throws Exception {
110-
logger.info("Delete container " + containerName + " by ID.");
110+
logger.info("Delete container {} by ID.", containerName);
111111

112112
// Delete container
113113
CosmosContainerResponse containerResp = database.getContainer(containerName).delete(new CosmosContainerRequestOptions());
@@ -118,7 +118,7 @@ private void deleteAContainer() throws Exception {
118118

119119
// Database delete
120120
private void deleteADatabase() throws Exception {
121-
logger.info("Last step: delete database " + databaseName + " by ID.");
121+
logger.info("Last step: delete database {} by ID.", databaseName);
122122

123123
// Delete database
124124
CosmosDatabaseResponse dbResp = client.getDatabase(databaseName).delete(new CosmosDatabaseRequestOptions());

0 commit comments

Comments
 (0)