Search in sources :

Example 26 with CosmosDatabaseResponse

use of com.azure.cosmos.models.CosmosDatabaseResponse in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class AutoscaleContainerCRUDQuickstart method createDatabaseIfNotExists.

// Database Create
private void createDatabaseIfNotExists() throws Exception {
    logger.info("Create database " + databaseName + " if not exists...");
    // Create database if not exists
    CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists(databaseName);
    database = client.getDatabase(databaseResponse.getProperties().getId());
    logger.info("Done.");
}
Also used : CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse)

Example 27 with CosmosDatabaseResponse

use of com.azure.cosmos.models.CosmosDatabaseResponse in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class AutoscaleDatabaseCRUDQuickstart method deleteADatabase.

// Database delete
private void deleteADatabase() throws Exception {
    logger.info("Last step: delete database " + databaseName + " by ID.");
    // Delete database
    CosmosDatabaseResponse dbResp = client.getDatabase(databaseName).delete(new CosmosDatabaseRequestOptions());
    logger.info("Status code for database delete: {}", dbResp.getStatusCode());
    logger.info("Done.");
}
Also used : CosmosDatabaseRequestOptions(com.azure.cosmos.models.CosmosDatabaseRequestOptions) CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse)

Example 28 with CosmosDatabaseResponse

use of com.azure.cosmos.models.CosmosDatabaseResponse in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SampleRequestThroughput method requestThroughputDemo.

public static void requestThroughputDemo() {
    client = new CosmosClientBuilder().endpoint(AccountSettings.HOST).key(AccountSettings.MASTER_KEY).consistencyLevel(ConsistencyLevel.EVENTUAL).contentResponseOnWriteEnabled(true).buildClient();
    // This code synchronously sends a request to create a database.
    // While the client waits for a response, this thread is blocked from
    // performing other tasks.
    CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists("ContosoInventoryDB");
    database = client.getDatabase(databaseResponse.getProperties().getId());
    logger.info("\n\n\n\nCreated database ContosoInventoryDB.\n\n\n\n");
    // IndexingPolicy indexingPolicy = new IndexingPolicy();
    // indexingPolicy.setIndexingMode(IndexingMode.NONE);
    // indexingPolicy.setAutomatic(false);
    CosmosContainerProperties containerProperties = new CosmosContainerProperties("ContosoInventoryContainer", "/id");
    // containerProperties.setIndexingPolicy(indexingPolicy);
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties, throughputProperties);
    container = database.getContainer(containerResponse.getProperties().getId());
    logger.info("\n\n\n\nCreated container ContosoInventoryContainer.\n\n\n\n");
    // Resources are ready.
    // 
    // Create many docs to insert into the container
    int number_of_docs = 50000;
    logger.info("Generating {} documents...", number_of_docs);
    ArrayList<JsonNode> docs = Profile.generateDocs(number_of_docs);
    logger.info("Inserting {} documents...", number_of_docs);
    Profile.tic();
    // Profiler code - it's good for this part to be async
    Flux.interval(Duration.ofMillis(10)).map(tick -> {
        // logger.info("In profiler.");
        toc_time = Profile.toc_ms();
        current_docs_inserted = number_docs_inserted.get();
        current_total_charge = total_charge.get();
        if (toc_time >= 1000.0) {
            Profile.tic();
            rps = 1000.0 * ((double) (current_docs_inserted - last_docs_inserted)) / toc_time;
            rups = 1000.0 * (current_total_charge - last_total_charge) / toc_time;
            logger.info(String.format("\n\n\n\n" + "Sync Throughput Profiler Result, Last 1000ms:" + "\n\n" + "%8s          %8s", StringUtils.center("Req/sec", 8), StringUtils.center("RU/s", 8)) + "\n" + "----------------------------------" + "\n" + String.format("%8.1f          %8.1f", rps, rups) + "\n\n\n\n");
            last_docs_inserted = current_docs_inserted;
            last_total_charge = current_total_charge;
        }
        return tick;
    }).subscribe();
    // While the client is waiting for a response, the thread is blocked from other tasks
    for (JsonNode doc : docs) {
        CosmosItemResponse<JsonNode> itemResponse = container.createItem(doc);
        if (itemResponse.getStatusCode() == 201) {
            number_docs_inserted.getAndIncrement();
            total_charge.getAndAdd((int) itemResponse.getRequestCharge());
        } else
            logger.warn("WARNING insert status code {} != 201", itemResponse.getStatusCode());
    }
    // Clean up
    logger.info("Deleting resources.");
    container.delete();
    database.delete();
    logger.info("Finished deleting resources.");
    logger.info("Closing client...");
    client.close();
    logger.info("Done with demo.");
}
Also used : Profile(com.azure.cosmos.examples.common.Profile) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) Logger(org.slf4j.Logger) CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) LoggerFactory(org.slf4j.LoggerFactory) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Flux(reactor.core.publisher.Flux) CosmosClient(com.azure.cosmos.CosmosClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CosmosContainer(com.azure.cosmos.CosmosContainer) CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) Duration(java.time.Duration) ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) JsonNode(com.fasterxml.jackson.databind.JsonNode) AccountSettings(com.azure.cosmos.examples.common.AccountSettings) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) JsonNode(com.fasterxml.jackson.databind.JsonNode) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 29 with CosmosDatabaseResponse

use of com.azure.cosmos.models.CosmosDatabaseResponse in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SampleStoredProcedure method setUp.

public void setUp() throws Exception {
    logger.info("Using Azure Cosmos DB endpoint: " + AccountSettings.HOST);
    ArrayList<String> preferredRegions = new ArrayList<String>();
    preferredRegions.add("West US");
    // Create sync client
    // <CreateSyncClient>
    client = new CosmosClientBuilder().endpoint(AccountSettings.HOST).key(AccountSettings.MASTER_KEY).preferredRegions(preferredRegions).consistencyLevel(ConsistencyLevel.EVENTUAL).contentResponseOnWriteEnabled(true).buildClient();
    logger.info("Create database " + databaseName + " with container " + containerName + " if either does not already exist.\n");
    CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists(databaseName);
    database = client.getDatabase(databaseResponse.getProperties().getId());
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/city");
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties, throughputProperties);
    container = database.getContainer(containerResponse.getProperties().getId());
}
Also used : CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse) ArrayList(java.util.ArrayList) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 30 with CosmosDatabaseResponse

use of com.azure.cosmos.models.CosmosDatabaseResponse in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SampleIndexManagement method createDatabaseIfNotExists.

private void createDatabaseIfNotExists() throws Exception {
    logger.info("Create database " + databaseName + " if not exists.");
    // Create database if not exists
    // <CreateDatabaseIfNotExists>
    CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists(databaseName);
    database = client.getDatabase(databaseResponse.getProperties().getId());
    // </CreateDatabaseIfNotExists>
    logger.info("Checking database " + database.getId() + " completed!\n");
}
Also used : CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse)

Aggregations

CosmosDatabaseResponse (com.azure.cosmos.models.CosmosDatabaseResponse)31 CosmosDatabaseRequestOptions (com.azure.cosmos.models.CosmosDatabaseRequestOptions)8 AfterAll (org.junit.jupiter.api.AfterAll)5 BeforeAll (org.junit.jupiter.api.BeforeAll)4 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)3 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)3 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)3 CosmosDiagnostics (com.azure.cosmos.CosmosDiagnostics)2 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)2 ArrayList (java.util.ArrayList)2 TypeManager (org.eclipse.dataspaceconnector.spi.types.TypeManager)2 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)1 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)1 CosmosClient (com.azure.cosmos.CosmosClient)1 CosmosContainer (com.azure.cosmos.CosmosContainer)1 CosmosDatabase (com.azure.cosmos.CosmosDatabase)1 AccountSettings (com.azure.cosmos.examples.common.AccountSettings)1 Profile (com.azure.cosmos.examples.common.Profile)1 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1