-
Notifications
You must be signed in to change notification settings - Fork 69
/
AnalyticalContainerCRUDQuickstart.java
144 lines (111 loc) · 4.87 KB
/
AnalyticalContainerCRUDQuickstart.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.cosmos.examples.analyticalcontainercrud.sync;
import com.azure.cosmos.ConsistencyLevel;
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosContainer;
import com.azure.cosmos.CosmosDatabase;
import com.azure.cosmos.examples.common.AccountSettings;
import com.azure.cosmos.models.CosmosContainerProperties;
import com.azure.cosmos.models.CosmosContainerRequestOptions;
import com.azure.cosmos.models.CosmosContainerResponse;
import com.azure.cosmos.models.CosmosDatabaseRequestOptions;
import com.azure.cosmos.models.CosmosDatabaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AnalyticalContainerCRUDQuickstart {
private CosmosClient client;
private final String databaseName = "AzureSampleFamilyDB";
private final String containerName = "FamilyContainer";
private CosmosDatabase database;
protected static Logger logger = LoggerFactory.getLogger(AnalyticalContainerCRUDQuickstart.class);
public void close() {
client.close();
}
/**
* Sample to demonstrate the following ANALYTICAL STORE container CRUD operations:
* -Create
* -Update throughput
* -Read by ID
* -Read all
* -Delete
*/
public static void main(String[] args) {
AnalyticalContainerCRUDQuickstart p = new AnalyticalContainerCRUDQuickstart();
try {
logger.info("Starting SYNC main");
p.containerCRUDDemo();
logger.info("Demo complete, please hold while resources are released");
} catch (Exception e) {
e.printStackTrace();
logger.error(String.format("Cosmos getStarted failed with %s", e));
} finally {
logger.info("Closing the client");
p.shutdown();
}
}
private void containerCRUDDemo() throws Exception {
logger.info("Using Azure Cosmos DB endpoint: {}", AccountSettings.HOST);
// Create sync client
client = new CosmosClientBuilder()
.endpoint(AccountSettings.HOST)
.key(AccountSettings.MASTER_KEY)
.consistencyLevel(ConsistencyLevel.EVENTUAL)
.contentResponseOnWriteEnabled(true)
.buildClient();
createDatabaseIfNotExists();
createContainerIfNotExists();
// deleteAContainer() is called at shutdown()
}
// Database Create
private void createDatabaseIfNotExists() throws Exception {
logger.info("Create database {} if not exists...", databaseName);
// Create database if not exists
CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists(databaseName);
database = client.getDatabase(databaseResponse.getProperties().getId());
logger.info("Done.");
}
// Container create
private void createContainerIfNotExists() throws Exception {
logger.info("Create container {} if not exists.", containerName);
// Create container if not exists
CosmosContainerProperties containerProperties =
new CosmosContainerProperties(containerName, "/lastName");
// Set analytical store properties
containerProperties.setAnalyticalStoreTimeToLiveInSeconds(-1);
// Create container
CosmosContainerResponse databaseResponse = database.createContainerIfNotExists(containerProperties);
CosmosContainer container = database.getContainer(databaseResponse.getProperties().getId());
logger.info("Done.");
}
// Container delete
private void deleteAContainer() throws Exception {
logger.info("Delete container {} by ID.", containerName);
// Delete container
CosmosContainerResponse containerResp = database.getContainer(containerName).delete(new CosmosContainerRequestOptions());
logger.info("Status code for container delete: {}",containerResp.getStatusCode());
logger.info("Done.");
}
// Database delete
private void deleteADatabase() throws Exception {
logger.info("Last step: delete database {} by ID.", databaseName);
// Delete database
CosmosDatabaseResponse dbResp = client.getDatabase(databaseName).delete(new CosmosDatabaseRequestOptions());
logger.info("Status code for database delete: {}",dbResp.getStatusCode());
logger.info("Done.");
}
// Cleanup before close
private void shutdown() {
try {
//Clean shutdown
deleteAContainer();
deleteADatabase();
} catch (Exception err) {
logger.error("Deleting Cosmos DB resources failed, will still attempt to close the client. See stack trace below.");
err.printStackTrace();
}
client.close();
logger.info("Done with sample.");
}
}