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

Commit 1af284d

Browse files
committed
Clean up code.
1 parent acc9f6c commit 1af284d

2 files changed

Lines changed: 45 additions & 37 deletions

File tree

src/main/java/com/azure/cosmos/examples/queries/async/QueriesQuickstartAsync.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ private void queriesDemo() throws Exception {
122122

123123
logger.info("Async doc create done.");
124124

125-
logger.info("Async doc create done.");
126-
127125
//execute all the below query examples asynchronously and waiting until all done
128126

129127
queryCrossPartitionAsyncUsingSessionConsistency();
@@ -464,7 +462,7 @@ private void queryWithQuerySpec() throws Exception {
464462
executeQueryWithQuerySpecPrintSingleResult(querySpec);
465463
}
466464

467-
private Map<String, String> upsertMultipleDocumentsCollectSessionTokens(String dbName, String containerName, int totalDocuments) {
465+
private Map<String, String> upsertMultipleDocumentsAndCollectSessionTokens(String dbName, String containerName, int totalDocuments) {
468466
Map<String, String> partitionToSessionToken = new ConcurrentHashMap<>();
469467

470468
// Use a dedicated client instance for creates/upserts
@@ -473,7 +471,6 @@ private Map<String, String> upsertMultipleDocumentsCollectSessionTokens(String
473471
.key(AccountSettings.MASTER_KEY)
474472
// Use SESSION consistency for session token semantics
475473
.consistencyLevel(ConsistencyLevel.SESSION)
476-
.contentResponseOnWriteEnabled(true)
477474
.buildAsyncClient()) {
478475

479476
CosmosAsyncContainer writerContainer = writerClient.getDatabase(dbName).getContainer(containerName);
@@ -509,15 +506,17 @@ private Map<String, String> upsertMultipleDocumentsCollectSessionTokens(String
509506
}
510507

511508
// Prepare upsert operations and attach callbacks to capture session tokens
512-
List<Mono<CosmosItemResponse<Family>>> upserts = new java.util.ArrayList<>();
509+
List<Mono<CosmosItemResponse<Family>>> upserts = new ArrayList<>();
510+
511+
CosmosItemRequestOptions requestOptionsForUpsert = new CosmosItemRequestOptions();
513512

514513
for (String pkValue : upsertPartitionKeys) {
515514
Family f = new Family();
516515
f.setLastName(pkValue);
517516
f.setId(UUID.randomUUID().toString());
518517

519518
Mono<CosmosItemResponse<Family>> upsertMono = writerContainer
520-
.upsertItem(f, new PartitionKey(pkValue), new CosmosItemRequestOptions())
519+
.upsertItem(f, new PartitionKey(pkValue), requestOptionsForUpsert)
521520
.map(response -> {
522521

523522
String sessionTokenFromUpsert = response.getSessionToken();
@@ -565,13 +564,12 @@ private void queryWithCompoundSessionTokenAndIteratePages(String dbName, String
565564
.key(AccountSettings.MASTER_KEY)
566565
// Use SESSION consistency to honor session tokens
567566
.consistencyLevel(ConsistencyLevel.SESSION)
568-
.contentResponseOnWriteEnabled(false)
569567
.buildAsyncClient()) {
570568

571569
CosmosAsyncContainer readerContainer = readerClient.getDatabase(dbName).getContainer(containerName);
572570

573571
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
574-
// Step 5: Plug in the compound session token
572+
// Plug in the compound session token
575573
options.setSessionToken(compoundSessionToken);
576574

577575
logger.info("Executing query with compound session token: {}", compoundSessionToken);
@@ -660,12 +658,12 @@ private void shutdown() {
660658

661659
// Steps:
662660
// 1. Create multiple documents (some with duplicate partition keys) and collect latest session tokens per partition key.
663-
// 2. Build a compound session token by concatenating the session tokens using commas present in partitionKeyToLastRecordedSessionToken.
661+
// 2. Build a compound session token by concatenating the session tokens using commas present as values in partitionKeyToLastRecordedSessionToken.
664662
// 3. Perform a cross-partition query using the compound session token.
665663
private void queryCrossPartitionAsyncUsingSessionConsistency() {
666-
logger.info("Starting cross-partition upserts and session-consistent query.");
664+
logger.info("Starting upserts across various logical partitions and a session-consistent query...");
667665
try {
668-
Map<String, String> partitionKeyToLastRecordedSessionToken = upsertMultipleDocumentsCollectSessionTokens(databaseName, containerName, 10);
666+
Map<String, String> partitionKeyToLastRecordedSessionToken = upsertMultipleDocumentsAndCollectSessionTokens(databaseName, containerName, 10);
669667

670668
String compoundSessionToken = buildCompoundSessionToken(partitionKeyToLastRecordedSessionToken);
671669

src/main/java/com/azure/cosmos/examples/queries/sync/QueriesQuickstart.java

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.azure.cosmos.examples.queries.sync;
55

66
import com.azure.cosmos.ConsistencyLevel;
7+
import com.azure.cosmos.CosmosAsyncClient;
78
import com.azure.cosmos.CosmosClient;
89
import com.azure.cosmos.CosmosClientBuilder;
910
import com.azure.cosmos.CosmosContainer;
@@ -529,41 +530,50 @@ private String buildCompoundSessionToken(Map<String, String> partitionSessionTok
529530
}
530531

531532
private void queryWithCompoundSessionTokenAndIteratePages(String databaseName, String containerName, String compoundSessionToken) {
532-
// This method is not used in this sample but illustrates how to use a compound session token
533-
// to execute a query and iterate over pages of results.
534-
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
535-
options.setSessionToken(compoundSessionToken);
533+
try (CosmosClient readerClient = new CosmosClientBuilder()
534+
.endpoint(AccountSettings.HOST)
535+
.key(AccountSettings.MASTER_KEY)
536+
// Use SESSION consistency to honor session tokens
537+
.consistencyLevel(ConsistencyLevel.SESSION)
538+
.buildClient()) {
536539

537-
String query = "SELECT * FROM c";
538-
int pageSize = 100;
539-
String continuationToken = null;
540-
int currentPageNumber = 1;
541-
double requestCharge = 0.0;
540+
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
541+
options.setSessionToken(compoundSessionToken);
542542

543-
do {
544-
logger.info("Receiving a set of query response pages (sync). ContinuationToken={}", continuationToken);
543+
CosmosContainer readerContainer = readerClient.getDatabase(databaseName).getContainer(containerName);
545544

546-
Iterable<FeedResponse<Family>> feedResponseIterator = container.queryItems(query, options, Family.class).iterableByPage(continuationToken, pageSize);
545+
String query = "SELECT * FROM c";
546+
int pageSize = 100;
547+
String continuationToken = null;
548+
int currentPageNumber = 1;
549+
double requestCharge = 0.0;
547550

548-
for (FeedResponse<Family> page : feedResponseIterator) {
549-
logger.info(String.format("Current page number: %d", currentPageNumber));
551+
do {
552+
logger.info("Receiving a set of query response pages (sync). ContinuationToken={}", continuationToken);
550553

551-
// Log the session token associated with this feed response
552-
logger.info("FeedResponse.sessionToken={}", page.getSessionToken());
554+
Iterable<FeedResponse<Family>> feedResponseIterator = readerContainer.queryItems(query, options, Family.class).iterableByPage(continuationToken, pageSize);
553555

554-
for (Family family : page.getResults()) {
555-
logger.info("Query result (sync): id={}, partitionKey={}", family.getId(), family.getLastName());
556-
}
556+
for (FeedResponse<Family> page : feedResponseIterator) {
557+
logger.info(String.format("Current page number: %d", currentPageNumber));
557558

558-
requestCharge += page.getRequestCharge();
559-
logger.info(String.format("Total request charge so far: %f\n", requestCharge));
559+
logger.info("FeedResponse.sessionToken={}", page.getSessionToken());
560560

561-
continuationToken = page.getContinuationToken();
562-
currentPageNumber++;
563-
}
561+
for (Family family : page.getResults()) {
562+
logger.info("Query result (sync): id={}, partitionKey={}", family.getId(), family.getLastName());
563+
}
564564

565-
} while (continuationToken != null);
565+
requestCharge += page.getRequestCharge();
566+
logger.info(String.format("Total request charge so far: %f\n", requestCharge));
567+
568+
continuationToken = page.getContinuationToken();
569+
currentPageNumber++;
570+
}
566571

567-
logger.info(String.format("Total request charge (sync): %f\n", requestCharge));
572+
} while (continuationToken != null);
573+
574+
logger.info(String.format("Total request charge (sync): %f\n", requestCharge));
575+
} catch (Exception e) {
576+
logger.error("Exception while querying with compound session token: {}", e.getMessage(), e);
577+
}
568578
}
569579
}

0 commit comments

Comments
 (0)