Skip to content

Commit

Permalink
Merge pull request #50 from TheovanKraay/add-async-samples
Browse files Browse the repository at this point in the history
add async samples
  • Loading branch information
TheovanKraay authored Aug 15, 2022
2 parents e1c7d07 + be796a8 commit 0ead4ca
Show file tree
Hide file tree
Showing 9 changed files with 2,001 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.cosmos.examples.analyticalcontainercrud.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.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 AnalyticalContainerCRUDQuickstartAsync {

private CosmosAsyncClient client;

private final String databaseName = "AzureSampleFamilyDB";
private final String containerName = "FamilyContainer";

private CosmosAsyncDatabase database;

protected static Logger logger = LoggerFactory.getLogger(AnalyticalContainerCRUDQuickstartAsync.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) {
AnalyticalContainerCRUDQuickstartAsync p = new AnalyticalContainerCRUDQuickstartAsync();

try {
logger.info("Starting ASYNC 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 async client
client = new CosmosClientBuilder()
.endpoint(AccountSettings.HOST)
.key(AccountSettings.MASTER_KEY)
.consistencyLevel(ConsistencyLevel.EVENTUAL)
.contentResponseOnWriteEnabled(true)
.buildAsyncClient();


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).block();
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).block();
CosmosAsyncContainer 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()).block();
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()).block();
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.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void main(String[] args) {

private void containerCRUDDemo() throws Exception {

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

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

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

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

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

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

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

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

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

// Delete database
CosmosDatabaseResponse dbResp = client.getDatabase(databaseName).delete(new CosmosDatabaseRequestOptions());
Expand Down
Loading

0 comments on commit 0ead4ca

Please sign in to comment.