Search in sources :

Example 1 with CosmosContainerRequestOptions

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

the class SampleChangeFeedProcessor method createNewLeaseCollection.

public static CosmosAsyncContainer createNewLeaseCollection(CosmosAsyncClient client, String databaseName, String leaseCollectionName) {
    CosmosAsyncDatabase databaseLink = client.getDatabase(databaseName);
    CosmosAsyncContainer leaseCollectionLink = databaseLink.getContainer(leaseCollectionName);
    CosmosContainerResponse leaseContainerResponse = null;
    try {
        leaseContainerResponse = leaseCollectionLink.read().block();
        if (leaseContainerResponse != null) {
            leaseCollectionLink.delete().block();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    } catch (RuntimeException ex) {
        if (ex instanceof CosmosException) {
            CosmosException CosmosException = (CosmosException) ex;
            if (CosmosException.getStatusCode() != 404) {
                throw ex;
            }
        } else {
            throw ex;
        }
    }
    CosmosContainerProperties containerSettings = new CosmosContainerProperties(leaseCollectionName, "/id");
    CosmosContainerRequestOptions requestOptions = new CosmosContainerRequestOptions();
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    leaseContainerResponse = databaseLink.createContainer(containerSettings, throughputProperties, requestOptions).block();
    if (leaseContainerResponse == null) {
        throw new RuntimeException(String.format("Failed to create collection %s in database %s.", leaseCollectionName, databaseName));
    }
    return databaseLink.getContainer(leaseContainerResponse.getProperties().getId());
}
Also used : CosmosContainerRequestOptions(com.azure.cosmos.models.CosmosContainerRequestOptions) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosException(com.azure.cosmos.CosmosException) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 2 with CosmosContainerRequestOptions

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

the class SampleChangeFeedProcessor method createNewCollection.

public static CosmosAsyncContainer createNewCollection(CosmosAsyncClient client, String databaseName, String collectionName) {
    CosmosAsyncDatabase databaseLink = client.getDatabase(databaseName);
    CosmosAsyncContainer collectionLink = databaseLink.getContainer(collectionName);
    CosmosContainerResponse containerResponse = null;
    try {
        containerResponse = collectionLink.read().block();
        if (containerResponse != null) {
            throw new IllegalArgumentException(String.format("Collection %s already exists in database %s.", collectionName, databaseName));
        }
    } catch (RuntimeException ex) {
        if (ex instanceof CosmosException) {
            CosmosException CosmosException = (CosmosException) ex;
            if (CosmosException.getStatusCode() != 404) {
                throw ex;
            }
        } else {
            throw ex;
        }
    }
    CosmosContainerProperties containerSettings = new CosmosContainerProperties(collectionName, "/pk");
    CosmosContainerRequestOptions requestOptions = new CosmosContainerRequestOptions();
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(10000);
    containerResponse = databaseLink.createContainer(containerSettings, throughputProperties, requestOptions).block();
    if (containerResponse == null) {
        throw new RuntimeException(String.format("Failed to create collection %s in database %s.", collectionName, databaseName));
    }
    return databaseLink.getContainer(containerResponse.getProperties().getId());
}
Also used : CosmosContainerRequestOptions(com.azure.cosmos.models.CosmosContainerRequestOptions) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosException(com.azure.cosmos.CosmosException) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 3 with CosmosContainerRequestOptions

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

the class AnalyticalContainerCRUDQuickstart method deleteAContainer.

// Container delete
private void deleteAContainer() throws Exception {
    logger.info("Delete container " + containerName + " by ID.");
    // Delete container
    CosmosContainerResponse containerResp = database.getContainer(containerName).delete(new CosmosContainerRequestOptions());
    logger.info("Status code for container delete: {}", containerResp.getStatusCode());
    logger.info("Done.");
}
Also used : CosmosContainerRequestOptions(com.azure.cosmos.models.CosmosContainerRequestOptions) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 4 with CosmosContainerRequestOptions

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

the class AutoscaleContainerCRUDQuickstart method deleteAContainer.

// Container delete
private void deleteAContainer() throws Exception {
    logger.info("Delete container " + containerName + " by ID.");
    // Delete container
    CosmosContainerResponse containerResp = database.getContainer(containerName).delete(new CosmosContainerRequestOptions());
    logger.info("Status code for container delete: {}", containerResp.getStatusCode());
    logger.info("Done.");
}
Also used : CosmosContainerRequestOptions(com.azure.cosmos.models.CosmosContainerRequestOptions) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 5 with CosmosContainerRequestOptions

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

the class AutoscaleContainerCRUDQuickstart method createContainerIfNotExists.

// Container create
private void createContainerIfNotExists() throws Exception {
    logger.info("Create autoscale container " + containerName + " if not exists.");
    // Container and autoscale throughput settings
    CosmosContainerProperties autoscaleContainerProperties = new CosmosContainerProperties(containerName, "/lastName");
    // Set autoscale max RU/s
    ThroughputProperties autoscaleThroughputProperties = ThroughputProperties.createAutoscaledThroughput(4000);
    // Create the container with autoscale enabled
    CosmosContainerResponse databaseResponse = database.createContainer(autoscaleContainerProperties, autoscaleThroughputProperties, new CosmosContainerRequestOptions());
    container = database.getContainer(databaseResponse.getProperties().getId());
    logger.info("Done.");
}
Also used : CosmosContainerRequestOptions(com.azure.cosmos.models.CosmosContainerRequestOptions) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Aggregations

CosmosContainerRequestOptions (com.azure.cosmos.models.CosmosContainerRequestOptions)7 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)7 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)5 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)5 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)4 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)4 CosmosException (com.azure.cosmos.CosmosException)4 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)2 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)2 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)2 AccountSettings (com.azure.cosmos.examples.common.AccountSettings)2 CosmosDatabaseResponse (com.azure.cosmos.models.CosmosDatabaseResponse)2 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)2 CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)2 PartitionKey (com.azure.cosmos.models.PartitionKey)2 CosmosPagedFlux (com.azure.cosmos.util.CosmosPagedFlux)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2