-
Notifications
You must be signed in to change notification settings - Fork 72
/
QueriesQuickstartAsync.java
492 lines (376 loc) · 19.7 KB
/
QueriesQuickstartAsync.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.cosmos.examples.queries.async;
import com.azure.cosmos.ConsistencyLevel;
import com.azure.cosmos.CosmosAsyncClient;
import com.azure.cosmos.CosmosAsyncContainer;
import com.azure.cosmos.CosmosAsyncDatabase;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.examples.common.AccountSettings;
import com.azure.cosmos.examples.common.Family;
import com.azure.cosmos.models.CosmosContainerProperties;
import com.azure.cosmos.models.CosmosContainerResponse;
import com.azure.cosmos.models.CosmosDatabaseRequestOptions;
import com.azure.cosmos.models.CosmosDatabaseResponse;
import com.azure.cosmos.models.CosmosItemRequestOptions;
import com.azure.cosmos.models.CosmosQueryRequestOptions;
import com.azure.cosmos.models.FeedResponse;
import com.azure.cosmos.models.PartitionKey;
import com.azure.cosmos.models.SqlParameter;
import com.azure.cosmos.models.SqlQuerySpec;
import com.azure.cosmos.models.ThroughputProperties;
import com.azure.cosmos.util.CosmosPagedFlux;
import com.fasterxml.jackson.databind.JsonNode;
import reactor.core.publisher.Flux;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
public class QueriesQuickstartAsync {
private CosmosAsyncClient client;
private final String databaseName = "AzureSampleFamilyDB";
private final String containerName = "FamilyContainer";
private final String documentId = UUID.randomUUID().toString();
private final String documentLastName = "Witherspoon";
private CosmosAsyncDatabase database;
private CosmosAsyncContainer container;
private static final Logger logger = LoggerFactory.getLogger(QueriesQuickstartAsync.class);
private AtomicInteger executeQueryPrintSingleResultNumber = new AtomicInteger(0);
private AtomicInteger executeCountQueryPrintSingleResultNumber = new AtomicInteger(0);
private AtomicInteger executeQueryWithQuerySpecPrintSingleResultNumber = new AtomicInteger(0);
private AtomicBoolean creatDocComplete = new AtomicBoolean(false);
public void close() {
client.close();
}
/**
* Sample to demonstrate Azure Cosmos DB queries via Java SQL API, including queries for:
* -All documents
* -Equality using =
* -Inequality using != and NOT
* -Using range operators like >, <, >=, <=
* -Using range operators against Strings
* -With ORDER BY
* -With aggregate functions
* -With subdocuments
* -With intra-document joins
* -With String, math and array operators
* -With parameterized SQL using SqlQuerySpec
* -With explicit paging
* -Query partitioned collections in parallel
* -With ORDER BY for partitioned collections
*/
public static void main(String[] args) {
QueriesQuickstartAsync p = new QueriesQuickstartAsync();
try {
logger.info("Starting ASYNC main");
p.queriesDemo();
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 queriesDemo() throws Exception {
logger.info("Using Azure Cosmos DB endpoint: {}", AccountSettings.HOST);
// Create async client
client = new CosmosClientBuilder()
.endpoint(AccountSettings.HOST)
.key(AccountSettings.MASTER_KEY)
.consistencyLevel(ConsistencyLevel.EVENTUAL)
.contentResponseOnWriteEnabled(true)
.buildAsyncClient();
createDatabaseIfNotExists();
createContainerIfNotExists();
createDocument();
// We are adding Thread.sleep to mimic the some business computation that can
// happen while waiting for earlier processes to finish.
Thread.sleep(1000);
logger.info("Async doc create done.");
//execute all the below query examples asynchronously and waiting until all done
queryAllDocuments();
queryWithPagingAndContinuationTokenAndPrintQueryCharge(new CosmosQueryRequestOptions());
queryEquality();
queryInequality();
queryRange();
queryRangeAgainstStrings();
queryOrderBy();
queryWithAggregateFunctions();
querySubdocuments();
queryIntraDocumentJoin();
queryStringMathAndArrayOperators();
queryWithQuerySpec();
parallelQueryWithPagingAndContinuationTokenAndPrintQueryCharge();
// We are adding Thread.sleep to mimic the some business computation that can
// happen while waiting for earlier processes to finish.
Thread.sleep(2000);
logger.info("*********Finished waiting - all async queries complete.");
// deleteDocument() is called at shutdown()
}
private void executeQueryPrintSingleResult(String sql) {
logger.info("Execute query {}",sql);
CosmosPagedFlux<Family> filteredFamilies = container.queryItems(sql, new CosmosQueryRequestOptions(), Family.class);
// Print
filteredFamilies.byPage(100).flatMap(filteredFamiliesResponse -> {
for (Family family : filteredFamiliesResponse.getResults()) {
logger.info(String.format("First query result: Family with (/id, partition key) = (%s,%s)",family.getId(),family.getLastName()));
}
executeQueryPrintSingleResultNumber.incrementAndGet();
return Flux.empty();
}).subscribe();
logger.info("The query: \"{}\" executed async and is done.", sql);
}
private void executeCountQueryPrintSingleResult(String sql) {
CosmosPagedFlux<JsonNode> filteredFamilies1 = container.queryItems(sql, new CosmosQueryRequestOptions(), JsonNode.class);
// Print
filteredFamilies1.byPage(100).flatMap(filteredFamiliesResponse -> {
for (JsonNode jsonnode : filteredFamiliesResponse.getResults()) {
logger.info("Count: {}", jsonnode.toString());
}
executeCountQueryPrintSingleResultNumber.incrementAndGet();
return Flux.empty();
}).subscribe();
logger.info("The query: \"{}\" executed async and is done.", sql);
}
private void executeQueryWithQuerySpecPrintSingleResult(SqlQuerySpec querySpec) {
logger.info("Execute query {}",querySpec.getQueryText());
CosmosPagedFlux<Family> filteredFamilies = container.queryItems(querySpec, new CosmosQueryRequestOptions(), Family.class);
// Print
filteredFamilies.byPage(100).flatMap(filteredFamiliesResponse -> {
for (Family family : filteredFamiliesResponse.getResults()) {
logger.info(String.format("First query result: Family with (/id, partition key) = (%s,%s)",family.getId(),family.getLastName()));
}
executeQueryWithQuerySpecPrintSingleResultNumber.incrementAndGet();
return Flux.empty();
}).subscribe();
logger.info("The query: \"{}\" executed async and is done.",querySpec.getQueryText());
}
// 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).block();
database = client.getDatabase(databaseResponse.getProperties().getId());
logger.info("createDatabaseIfNotExists done synchronously.");
}
// 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");
// Provision throughput
ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
// Create container with 200 RU/s
CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties, throughputProperties).block();
container = database.getContainer(containerResponse.getProperties().getId());
logger.info("createContainerIfNotExists done synchronously.");
}
private void createDocument() throws Exception {
logger.info("Create document {}", documentId);
// Define a document as a POJO (internally this
// is converted to JSON via custom serialization)
Family family = new Family();
family.setLastName(documentLastName);
family.setId(documentId);
// Insert this item as a document
// Explicitly specifying the /pk value improves performance.
container.createItem(family, new PartitionKey(family.getLastName()), new CosmosItemRequestOptions())
.doOnSuccess((response) -> {
logger.info("created doc with id: {}", response.getItem().getId());
this.creatDocComplete.set(true);
})
.doOnError((exception) -> {
logger.error(
"Exception. e: {}",
exception.getLocalizedMessage(),
exception);
}).subscribe();
}
private void queryAllDocuments() throws Exception {
logger.info("Query all documents.");
executeQueryPrintSingleResult("SELECT * FROM c");
}
private void queryWithPagingAndContinuationTokenAndPrintQueryCharge(CosmosQueryRequestOptions options) throws Exception {
logger.info("Query with paging and continuation token; print the total RU charge of the query");
String query = "SELECT * FROM Families";
int pageSize = 100; //No of docs per page
int currentPageNumber = 1;
int documentNumber = 0;
String continuationToken = null;
double requestCharge = 0.0;
// First iteration (continuationToken = null): Receive a batch of query response pages
// Subsequent iterations (continuationToken != null): Receive subsequent batch of query response pages, with continuationToken indicating where the previous iteration left off
do {
logger.info("Receiving a set of query response pages.");
logger.info("Continuation Token: {}\n", continuationToken);
CosmosQueryRequestOptions queryOptions = new CosmosQueryRequestOptions();
Iterable<FeedResponse<Family>> feedResponseIterator =
container.queryItems(query, queryOptions, Family.class).byPage(continuationToken,pageSize).toIterable();
for (FeedResponse<Family> page : feedResponseIterator) {
logger.info(String.format("Current page number: %d", currentPageNumber));
// Access all of the documents in this result page
for (Family docProps : page.getResults()) {
documentNumber++;
}
// Accumulate the request charge of this page
requestCharge += page.getRequestCharge();
// Page count so far
logger.info(String.format("Total documents received so far: %d", documentNumber));
// Request charge so far
logger.info(String.format("Total request charge so far: %f\n", requestCharge));
// Along with page results, get a continuation token
// which enables the client to "pick up where it left off"
// in accessing query response pages.
continuationToken = page.getContinuationToken();
currentPageNumber++;
}
} while (continuationToken != null);
logger.info(String.format("Total request charge: %f\n", requestCharge));
}
private void parallelQueryWithPagingAndContinuationTokenAndPrintQueryCharge() throws Exception {
logger.info("Parallel implementation of:");
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
// 0 maximum parallel tasks, effectively serial execution
options.setMaxDegreeOfParallelism(0);
options.setMaxBufferedItemCount(100);
queryWithPagingAndContinuationTokenAndPrintQueryCharge(options);
// 1 maximum parallel tasks, 1 dedicated asynchronous task to continuously make REST calls
options.setMaxDegreeOfParallelism(1);
options.setMaxBufferedItemCount(100);
queryWithPagingAndContinuationTokenAndPrintQueryCharge(options);
// 10 maximum parallel tasks, a maximum of 10 dedicated asynchronous tasks to continuously make REST calls
options.setMaxDegreeOfParallelism(10);
options.setMaxBufferedItemCount(100);
queryWithPagingAndContinuationTokenAndPrintQueryCharge(options);
logger.info("Done with parallel queries.");
}
private void queryEquality() throws Exception {
logger.info("Query for equality using =");
executeQueryPrintSingleResult("SELECT * FROM c WHERE c.id = '" + documentId + "'");
}
private void queryInequality() throws Exception {
logger.info("Query for inequality");
executeQueryPrintSingleResult("SELECT * FROM c WHERE c.id != '" + documentId + "'");
executeQueryPrintSingleResult("SELECT * FROM c WHERE c.id <> '" + documentId + "'");
// Combine equality and inequality
executeQueryPrintSingleResult("SELECT * FROM c WHERE c.lastName = '" + documentLastName + "' AND c.id != '" + documentId + "'");
}
private void queryRange() throws Exception {
logger.info("Numerical range query");
// Numerical range query
executeQueryPrintSingleResult("SELECT * FROM Families f WHERE f.Children[0].Grade > 5");
}
private void queryRangeAgainstStrings() throws Exception {
logger.info("String range query");
// String range query
executeQueryPrintSingleResult("SELECT * FROM Families f WHERE f.Address.State > 'NY'");
}
private void queryOrderBy() throws Exception {
logger.info("ORDER BY queries");
// Numerical ORDER BY
executeQueryPrintSingleResult("SELECT * FROM Families f WHERE f.LastName = 'Andersen' ORDER BY f.Children[0].Grade");
}
private void queryDistinct() throws Exception {
logger.info("DISTINCT queries");
// DISTINCT query
executeQueryPrintSingleResult("SELECT DISTINCT c.lastName from c");
}
private void queryWithAggregateFunctions() throws Exception {
logger.info("Aggregate function queries");
// Basic query with aggregate functions
executeCountQueryPrintSingleResult("SELECT VALUE COUNT(f) FROM Families f WHERE f.LastName = 'Andersen'");
// Query with aggregate functions within documents
executeCountQueryPrintSingleResult("SELECT VALUE COUNT(child) FROM child IN f.Children");
}
private void querySubdocuments() throws Exception {
// Cosmos DB supports the selection of sub-documents on the server, there
// is no need to send down the full family record if all you want to display
// is a single child
logger.info("Subdocument query");
executeQueryPrintSingleResult("SELECT VALUE c FROM c IN f.Children");
}
private void queryIntraDocumentJoin() throws Exception {
// Cosmos DB supports the notion of an Intra-document Join, or a self-join
// which will effectively flatten the hierarchy of a document, just like doing
// a self JOIN on a SQL table
logger.info("Intra-document joins");
// Single join
executeQueryPrintSingleResult("SELECT f.id FROM Families f JOIN c IN f.Children");
// Two joins
executeQueryPrintSingleResult("SELECT f.id as family, c.FirstName AS child, p.GivenName AS pet " +
"FROM Families f " +
"JOIN c IN f.Children " +
"join p IN c.Pets");
// Two joins and a filter
executeQueryPrintSingleResult("SELECT f.id as family, c.FirstName AS child, p.GivenName AS pet " +
"FROM Families f " +
"JOIN c IN f.Children " +
"join p IN c.Pets " +
"WHERE p.GivenName = 'Fluffy'");
}
private void queryStringMathAndArrayOperators() throws Exception {
logger.info("Queries with string, math and array operators");
// String STARTSWITH operator
executeQueryPrintSingleResult("SELECT * FROM family WHERE STARTSWITH(family.LastName, 'An')");
// Round down numbers with FLOOR
executeQueryPrintSingleResult("SELECT VALUE FLOOR(family.Children[0].Grade) FROM family");
// Get number of children using array length
executeQueryPrintSingleResult("SELECT VALUE ARRAY_LENGTH(family.Children) FROM family");
}
private void queryWithQuerySpec() throws Exception {
logger.info("Query with SqlQuerySpec");
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
options.setPartitionKey(new PartitionKey("Witherspoon"));
// Simple query with a single property equality comparison
// in SQL with SQL parameterization instead of inlining the
// parameter values in the query string
ArrayList<SqlParameter> paramList = new ArrayList<SqlParameter>();
paramList.add(new SqlParameter("@id", "AndersenFamily"));
SqlQuerySpec querySpec = new SqlQuerySpec(
"SELECT * FROM Families f WHERE (f.id = @id)",
paramList);
executeQueryWithQuerySpecPrintSingleResult(querySpec);
// Query using two properties within each document. WHERE Id = "" AND Address.City = ""
// notice here how we are doing an equality comparison on the string value of City
paramList = new ArrayList<SqlParameter>();
paramList.add(new SqlParameter("@id", "AndersenFamily"));
paramList.add(new SqlParameter("@city", "Seattle"));
querySpec = new SqlQuerySpec(
"SELECT * FROM Families f WHERE f.id = @id AND f.Address.City = @city",
paramList);
executeQueryWithQuerySpecPrintSingleResult(querySpec);
}
// Document delete
private void deleteADocument() throws Exception {
logger.info("Delete document {} by ID.", documentId);
// Delete document
container.deleteItem(documentId, new PartitionKey(documentLastName), new CosmosItemRequestOptions()).block();
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()).block();
logger.info("Status code for database delete: {}",dbResp.getStatusCode());
logger.info("Done.");
}
// Cleanup before close
private void shutdown() {
try {
//Clean shutdown
deleteADocument();
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.");
}
}