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