Search in sources :

Example 1 with PartitionKeyDefinition

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

the class SampleSubpartitioningAsync method createContainerIfNotExists.

private void createContainerIfNotExists() throws Exception {
    logger.info("Create container " + containerName + " if not exists.");
    // Create container if not exists
    // <Create PartitionKeyDefinition>
    List<String> partitionKeyPaths = new ArrayList<String>();
    partitionKeyPaths.add("/tenantId");
    partitionKeyPaths.add("/userId");
    partitionKeyPaths.add("/sessionId");
    PartitionKeyDefinition subpartitionKeyDefinition = new PartitionKeyDefinition();
    subpartitionKeyDefinition.setPaths(partitionKeyPaths);
    subpartitionKeyDefinition.setKind(PartitionKind.MULTI_HASH);
    subpartitionKeyDefinition.setVersion(PartitionKeyDefinitionVersion.V2);
    // <CreateContainerIfNotExists>
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, subpartitionKeyDefinition);
    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) SampleCRUDQuickstartAsync(com.azure.cosmos.examples.crudquickstart.async.SampleCRUDQuickstartAsync) ArrayList(java.util.ArrayList) CosmosContainerRequestOptions(com.azure.cosmos.models.CosmosContainerRequestOptions) 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) PartitionKind(com.azure.cosmos.models.PartitionKind) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) Logger(org.slf4j.Logger) UserSessionData(com.azure.cosmos.examples.common.UserSessionData) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) PartitionKeyDefinitionVersion(com.azure.cosmos.models.PartitionKeyDefinitionVersion) Mono(reactor.core.publisher.Mono) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) Collectors(java.util.stream.Collectors) PartitionKeyBuilder(com.azure.cosmos.models.PartitionKeyBuilder) UserSession(com.azure.cosmos.examples.common.UserSession) Flux(reactor.core.publisher.Flux) List(java.util.List) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) PartitionKeyDefinition(com.azure.cosmos.models.PartitionKeyDefinition) 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) ArrayList(java.util.ArrayList) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) PartitionKeyDefinition(com.azure.cosmos.models.PartitionKeyDefinition) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Aggregations

ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)1 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)1 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)1 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)1 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)1 CosmosException (com.azure.cosmos.CosmosException)1 AccountSettings (com.azure.cosmos.examples.common.AccountSettings)1 UserSession (com.azure.cosmos.examples.common.UserSession)1 UserSessionData (com.azure.cosmos.examples.common.UserSessionData)1 SampleCRUDQuickstartAsync (com.azure.cosmos.examples.crudquickstart.async.SampleCRUDQuickstartAsync)1 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)1 CosmosContainerRequestOptions (com.azure.cosmos.models.CosmosContainerRequestOptions)1 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)1 CosmosDatabaseResponse (com.azure.cosmos.models.CosmosDatabaseResponse)1 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)1 CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)1 PartitionKey (com.azure.cosmos.models.PartitionKey)1 PartitionKeyBuilder (com.azure.cosmos.models.PartitionKeyBuilder)1 PartitionKeyDefinition (com.azure.cosmos.models.PartitionKeyDefinition)1 PartitionKeyDefinitionVersion (com.azure.cosmos.models.PartitionKeyDefinitionVersion)1