Search in sources :

Example 16 with ThroughputProperties

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

the class SalesOrder method MigrateJavaSDKv4ContainerTTLAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/migrate-java-v4-sdk
 * Container TTL
 */
/**
 * Container TTL
 */
public static void MigrateJavaSDKv4ContainerTTLAsync() {
    String hostName = "hostname";
    String partition_key = "/pk";
    CosmosAsyncDatabase database = null;
    // <MigrateContainerTTLAsync>
    CosmosAsyncContainer container;
    // Create a new container with TTL enabled with default expiration value
    CosmosContainerProperties containerProperties = new CosmosContainerProperties("myContainer", "/myPartitionKey");
    containerProperties.setDefaultTimeToLiveInSeconds(90 * 60 * 60 * 24);
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    database.createContainerIfNotExists(containerProperties, throughputProperties).block();
    container = database.getContainer("myContainer");
// </MigrateContainerTTLAsync>
}
Also used : ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties)

Example 17 with ThroughputProperties

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

the class SampleRequestThroughputAsync method requestThroughputDemo.

public static void requestThroughputDemo() {
    // Create Async client.
    // Building an async client is still a sync operation.
    client = new CosmosClientBuilder().endpoint(AccountSettings.HOST).key(AccountSettings.MASTER_KEY).consistencyLevel(ConsistencyLevel.EVENTUAL).contentResponseOnWriteEnabled(true).buildAsyncClient();
    // Describe the logic of database and container creation using Reactor...
    Mono<Void> databaseContainerIfNotExist = client.createDatabaseIfNotExists("ContosoInventoryDB").flatMap(databaseResponse -> {
        database = client.getDatabase(databaseResponse.getProperties().getId());
        logger.info("\n\n\n\nCreated database ContosoInventoryDB.\n\n\n\n");
        CosmosContainerProperties containerProperties = new CosmosContainerProperties("ContosoInventoryContainer", "/id");
        ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
        return database.createContainerIfNotExists(containerProperties, throughputProperties);
    }).flatMap(containerResponse -> {
        container = database.getContainer(containerResponse.getProperties().getId());
        logger.info("\n\n\n\nCreated container ContosoInventoryContainer.\n\n\n\n");
        return Mono.empty();
    });
    // ...it doesn't execute until you subscribe().
    // The async call returns immediately...
    logger.info("Creating database and container asynchronously...");
    databaseContainerIfNotExist.subscribe(voidItem -> {
    }, err -> {
    }, () -> {
        logger.info("Finished creating resources.\n\n");
        resources_created.set(true);
    });
    // ...so we can do other things until async response arrives!
    logger.info("Doing other things until async resource creation completes......");
    while (!resources_created.get()) Profile.doOtherThings();
    // Container is created. Generate many docs to insert.
    int number_of_docs = 50000;
    logger.info("Generating {} documents...", number_of_docs);
    ArrayList<JsonNode> docs = Profile.generateDocs(number_of_docs);
    // Insert many docs into container...
    logger.info("Inserting {} documents...", number_of_docs);
    Profile.tic();
    int last_docs_inserted = 0;
    double last_total_charge = 0.0;
    Flux.fromIterable(docs).flatMap(doc -> container.createItem(doc)).flatMap(itemResponse -> {
        if (itemResponse.getStatusCode() == 201) {
            number_docs_inserted.getAndIncrement();
            total_charge.getAndAdd((int) (itemResponse.getRequestCharge()));
        } else
            logger.warn("WARNING insert status code {} != 201", itemResponse.getStatusCode());
        return Mono.empty();
    }).subscribe();
    // Do other things until async response arrives
    logger.info("Doing other things until async doc inserts complete...");
    // while (number_docs_inserted.get() < number_of_docs) Profile.doOtherThings();
    double toc_time = 0.0;
    int current_docs_inserted = 0;
    double current_total_charge = 0.0, rps = 0.0, rups = 0.0;
    while (number_docs_inserted.get() < number_of_docs) {
        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" + "Async 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;
        }
    }
    // Inserts are complete. Cleanup (asynchronously!)
    logger.info("Deleting resources.");
    container.delete().flatMap(containerResponse -> database.delete()).subscribe(dbItem -> {
    }, err -> {
    }, () -> {
        logger.info("Finished deleting resources.");
        resources_deleted.set(true);
    });
    // Do other things until async response arrives
    logger.info("Do other things until async resource delete completes...");
    while (!resources_deleted.get()) Profile.doOtherThings();
    // Close client. This is always sync.
    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) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Mono(reactor.core.publisher.Mono) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Flux(reactor.core.publisher.Flux) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) 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) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 18 with ThroughputProperties

use of com.azure.cosmos.models.ThroughputProperties 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 19 with ThroughputProperties

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

the class SampleStoredProcedureAsync 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).buildAsyncClient();
    logger.info("Create database " + databaseName + " with container " + containerName + " if either does not already exist.\n");
    client.createDatabaseIfNotExists(databaseName).flatMap(databaseResponse -> {
        database = client.getDatabase(databaseResponse.getProperties().getId());
        return Mono.empty();
    }).block();
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/id");
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    database.createContainerIfNotExists(containerProperties, throughputProperties).flatMap(containerResponse -> {
        container = database.getContainer(containerResponse.getProperties().getId());
        return Mono.empty();
    }).block();
}
Also used : CustomPOJO(com.azure.cosmos.examples.common.CustomPOJO) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) Logger(org.slf4j.Logger) PartitionKey(com.azure.cosmos.models.PartitionKey) LoggerFactory(org.slf4j.LoggerFactory) Mono(reactor.core.publisher.Mono) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) CosmosException(com.azure.cosmos.CosmosException) CosmosPagedFlux(com.azure.cosmos.util.CosmosPagedFlux) AccountSettings(com.azure.cosmos.examples.common.AccountSettings) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) ArrayList(java.util.ArrayList) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties)

Example 20 with ThroughputProperties

use of com.azure.cosmos.models.ThroughputProperties 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)

Aggregations

ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)24 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)22 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)15 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)11 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)11 ArrayList (java.util.ArrayList)10 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)9 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)7 CosmosException (com.azure.cosmos.CosmosException)7 AccountSettings (com.azure.cosmos.examples.common.AccountSettings)7 Mono (reactor.core.publisher.Mono)7 CosmosDatabaseResponse (com.azure.cosmos.models.CosmosDatabaseResponse)6 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)6 Flux (reactor.core.publisher.Flux)6 CosmosContainerRequestOptions (com.azure.cosmos.models.CosmosContainerRequestOptions)5 PartitionKey (com.azure.cosmos.models.PartitionKey)5 CosmosPagedFlux (com.azure.cosmos.util.CosmosPagedFlux)5