Search in sources :

Example 16 with CosmosContainerResponse

use of com.azure.cosmos.models.CosmosContainerResponse in project scalardb by scalar-labs.

the class CosmosAdmin method updateIndexingPolicy.

private void updateIndexingPolicy(String databaseName, String containerName, TableMetadata newTableMetadata) throws ExecutionException {
    CosmosDatabase database = client.getDatabase(databaseName);
    try {
        // get the existing container properties
        CosmosContainerResponse response = database.createContainerIfNotExists(containerName, PARTITION_KEY_PATH);
        CosmosContainerProperties properties = response.getProperties();
        // set the new index policy to the container properties
        properties.setIndexingPolicy(computeIndexingPolicy(newTableMetadata));
        // update the container properties
        database.getContainer(containerName).replace(properties);
    } catch (RuntimeException e) {
        throw new ExecutionException("updating the indexing policy failed", e);
    }
}
Also used : CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) ExecutionException(com.scalar.db.exception.storage.ExecutionException) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 17 with CosmosContainerResponse

use of com.azure.cosmos.models.CosmosContainerResponse in project DataSpaceConnector by eclipse-dataspaceconnector.

the class CosmosDbApiImplIntegrationTest method setup.

@BeforeEach
void setup() {
    assertThat(database).describedAs("CosmosDB database is null - did something go wrong during initialization?").isNotNull();
    CosmosContainerResponse containerIfNotExists = database.createContainerIfNotExists(CONTAINER_NAME, "/partitionKey");
    container = database.getContainer(containerIfNotExists.getProperties().getId());
    cosmosDbApi = new CosmosDbApiImpl(container, true);
    record = new ArrayList<>();
}
Also used : CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) CosmosDbApiImpl(org.eclipse.dataspaceconnector.azure.cosmos.CosmosDbApiImpl) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 18 with CosmosContainerResponse

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

the class SampleCRUDQuickstartAsync method createContainerIfNotExists.

private void createContainerIfNotExists() throws Exception {
    logger.info("Create container " + containerName + " if not exists.");
    // Create container if not exists
    // <CreateContainerIfNotExists>
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/lastName");
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    Mono<CosmosContainerResponse> containerIfNotExists = database.createContainerIfNotExists(containerProperties, throughputProperties);
    // Create container with 400 RU/s
    CosmosContainerResponse cosmosContainerResponse = containerIfNotExists.block();
    container = database.getContainer(cosmosContainerResponse.getProperties().getId());
    // </CreateContainerIfNotExists>
    // Modify existing container
    containerProperties = cosmosContainerResponse.getProperties();
    Mono<CosmosContainerResponse> propertiesReplace = container.replace(containerProperties, new CosmosContainerRequestOptions());
    propertiesReplace.flatMap(containerResponse -> {
        logger.info("setupContainer(): Container " + container.getId() + " in " + database.getId() + "has been updated with it's new properties.");
        return Mono.empty();
    }).onErrorResume((exception) -> {
        logger.error("setupContainer(): Unable to update properties for container " + container.getId() + " in database " + database.getId() + ". e: " + exception.getLocalizedMessage());
        return Mono.empty();
    }).block();
}
Also used : CosmosContainerRequestOptions(com.azure.cosmos.models.CosmosContainerRequestOptions) PartitionKey(com.azure.cosmos.models.PartitionKey) LoggerFactory(org.slf4j.LoggerFactory) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) ArrayList(java.util.ArrayList) CosmosContainerRequestOptions(com.azure.cosmos.models.CosmosContainerRequestOptions) Families(com.azure.cosmos.examples.common.Families) Family(com.azure.cosmos.examples.common.Family) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) Duration(java.time.Duration) ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) CosmosException(com.azure.cosmos.CosmosException) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) Logger(org.slf4j.Logger) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) Mono(reactor.core.publisher.Mono) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) Collectors(java.util.stream.Collectors) Flux(reactor.core.publisher.Flux) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse) CosmosPagedFlux(com.azure.cosmos.util.CosmosPagedFlux) AccountSettings(com.azure.cosmos.examples.common.AccountSettings) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 19 with CosmosContainerResponse

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

the class CosmosDiagnosticsQuickStartAsync method createContainerIfNotExists.

// Container create
private void createContainerIfNotExists() throws Exception {
    logger.info("Creating container {} if not exists", containerName);
    // Create container if not exists
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/lastName");
    // Provision throughput
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    // Create container with 200 RU/s
    Mono<CosmosContainerResponse> containerResponseMono = database.createContainerIfNotExists(containerProperties, throughputProperties);
    CosmosContainerResponse cosmosContainerResponse = containerResponseMono.block();
    CosmosDiagnostics diagnostics = cosmosContainerResponse.getDiagnostics();
    logger.info("Create container diagnostics : {}", diagnostics);
    container = database.getContainer(cosmosContainerResponse.getProperties().getId());
    logger.info("Done.");
}
Also used : ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosDiagnostics(com.azure.cosmos.CosmosDiagnostics) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 20 with CosmosContainerResponse

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

Aggregations

CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)25 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)19 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)15 CosmosContainerRequestOptions (com.azure.cosmos.models.CosmosContainerRequestOptions)7 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)6 CosmosDatabaseResponse (com.azure.cosmos.models.CosmosDatabaseResponse)6 ArrayList (java.util.ArrayList)6 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)5 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)5 CosmosException (com.azure.cosmos.CosmosException)5 PartitionKey (com.azure.cosmos.models.PartitionKey)5 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)4 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)4 CosmosContainer (com.azure.cosmos.CosmosContainer)4 CosmosDatabase (com.azure.cosmos.CosmosDatabase)4 AccountSettings (com.azure.cosmos.examples.common.AccountSettings)4 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)4 IndexingPolicy (com.azure.cosmos.models.IndexingPolicy)4 Duration (java.time.Duration)4 CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)3