Search in sources :

Example 21 with CosmosContainerResponse

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

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

the class SampleIndexManagementAsync method createContainerIfNotExistsWithSpecifiedIndex.

private void createContainerIfNotExistsWithSpecifiedIndex() throws Exception {
    logger.info("Create container " + containerName + " if not exists.");
    // Create container if not exists
    // <CreateContainerIfNotExists>
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/lastName");
    // <CustomIndexingPolicy>
    IndexingPolicy indexingPolicy = new IndexingPolicy();
    // To turn indexing off set IndexingMode.NONE
    indexingPolicy.setIndexingMode(IndexingMode.CONSISTENT);
    // Included paths
    List<IncludedPath> includedPaths = new ArrayList<>();
    includedPaths.add(new IncludedPath("/*"));
    indexingPolicy.setIncludedPaths(includedPaths);
    // Excluded paths
    List<ExcludedPath> excludedPaths = new ArrayList<>();
    excludedPaths.add(new ExcludedPath("/name/*"));
    indexingPolicy.setExcludedPaths(excludedPaths);
    // Spatial indices - if you need them, here is how to set them up:
    /*
        List<SpatialSpec> spatialIndexes = new ArrayList<SpatialSpec>();
        List<SpatialType> collectionOfSpatialTypes = new ArrayList<SpatialType>();

        SpatialSpec spec = new SpatialSpec();
        spec.setPath("/locations/*");
        collectionOfSpatialTypes.add(SpatialType.Point);
        spec.setSpatialTypes(collectionOfSpatialTypes);
        spatialIndexes.add(spec);

        indexingPolicy.setSpatialIndexes(spatialIndexes);
         */
    // Composite indices - if you need them, here is how to set them up:
    /*
        List<List<CompositePath>> compositeIndexes = new ArrayList<>();
        List<CompositePath> compositePaths = new ArrayList<>();

        CompositePath nameCompositePath = new CompositePath();
        nameCompositePath.setPath("/name");
        nameCompositePath.setOrder(CompositePathSortOrder.ASCENDING);

        CompositePath ageCompositePath = new CompositePath();
        ageCompositePath.setPath("/age");
        ageCompositePath.setOrder(CompositePathSortOrder.DESCENDING);

        compositePaths.add(ageCompositePath);
        compositePaths.add(nameCompositePath);

        compositeIndexes.add(compositePaths);
        indexingPolicy.setCompositeIndexes(compositeIndexes);
         */
    containerProperties.setIndexingPolicy(indexingPolicy);
    // </CustomIndexingPolicy>
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    Mono<CosmosContainerResponse> containerIfNotExists = database.createContainerIfNotExists(containerProperties, throughputProperties);
    // Create container with 400 RU/s
    containerIfNotExists.flatMap(containerResponse -> {
        container = database.getContainer(containerResponse.getProperties().getId());
        logger.info("Checking container " + container.getId() + " completed!\n");
        return Mono.empty();
    }).block();
// </CreateContainerIfNotExists>
}
Also used : PartitionKey(com.azure.cosmos.models.PartitionKey) LoggerFactory(org.slf4j.LoggerFactory) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) ExcludedPath(com.azure.cosmos.models.ExcludedPath) ArrayList(java.util.ArrayList) 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) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy) Logger(org.slf4j.Logger) IncludedPath(com.azure.cosmos.models.IncludedPath) IndexingMode(com.azure.cosmos.models.IndexingMode) 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) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) 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) ArrayList(java.util.ArrayList) IncludedPath(com.azure.cosmos.models.IncludedPath) ExcludedPath(com.azure.cosmos.models.ExcludedPath) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 23 with CosmosContainerResponse

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

the class SampleIndexManagement method createContainerIfNotExistsWithSpecifiedIndex.

private void createContainerIfNotExistsWithSpecifiedIndex() throws Exception {
    logger.info("Create container " + containerName + " if not exists.");
    // Create container if not exists
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/lastName");
    // <CustomIndexingPolicy>
    IndexingPolicy indexingPolicy = new IndexingPolicy();
    // To turn indexing off set IndexingMode.NONE
    indexingPolicy.setIndexingMode(IndexingMode.CONSISTENT);
    // Included paths
    List<IncludedPath> includedPaths = new ArrayList<>();
    includedPaths.add(new IncludedPath("/*"));
    indexingPolicy.setIncludedPaths(includedPaths);
    // Excluded paths
    List<ExcludedPath> excludedPaths = new ArrayList<>();
    excludedPaths.add(new ExcludedPath("/name/*"));
    indexingPolicy.setExcludedPaths(excludedPaths);
    // Spatial indices - if you need them, here is how to set them up:
    /*
        List<SpatialSpec> spatialIndexes = new ArrayList<SpatialSpec>();
        List<SpatialType> collectionOfSpatialTypes = new ArrayList<SpatialType>();

        SpatialSpec spec = new SpatialSpec();
        spec.setPath("/locations/*");
        collectionOfSpatialTypes.add(SpatialType.Point);
        spec.setSpatialTypes(collectionOfSpatialTypes);
        spatialIndexes.add(spec);

        indexingPolicy.setSpatialIndexes(spatialIndexes);
         */
    // Composite indices - if you need them, here is how to set them up:
    /*
        List<List<CompositePath>> compositeIndexes = new ArrayList<>();
        List<CompositePath> compositePaths = new ArrayList<>();

        CompositePath nameCompositePath = new CompositePath();
        nameCompositePath.setPath("/name");
        nameCompositePath.setOrder(CompositePathSortOrder.ASCENDING);

        CompositePath ageCompositePath = new CompositePath();
        ageCompositePath.setPath("/age");
        ageCompositePath.setOrder(CompositePathSortOrder.DESCENDING);

        compositePaths.add(ageCompositePath);
        compositePaths.add(nameCompositePath);

        compositeIndexes.add(compositePaths);
        indexingPolicy.setCompositeIndexes(compositeIndexes);
         */
    containerProperties.setIndexingPolicy(indexingPolicy);
    // </CustomIndexingPolicy>
    // Create container with 400 RU/s
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties, throughputProperties);
    container = database.getContainer(containerResponse.getProperties().getId());
    logger.info("Checking container " + container.getId() + " completed!\n");
}
Also used : ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) ArrayList(java.util.ArrayList) IncludedPath(com.azure.cosmos.models.IncludedPath) ExcludedPath(com.azure.cosmos.models.ExcludedPath) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 24 with CosmosContainerResponse

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

the class SamplePatchQuickstart method createContainerIfNotExists.

private void createContainerIfNotExists() throws Exception {
    logger.info("Create container {} if not exists.", containerName);
    // Create container if not exists
    // <CreateContainerIfNotExists>
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/lastName");
    // Create container with 400 RU/s
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties, throughputProperties);
    container = database.getContainer(containerResponse.getProperties().getId());
    // </CreateContainerIfNotExists>
    logger.info("Checking container {} completed!\n", container.getId());
}
Also used : ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 25 with CosmosContainerResponse

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

the class QueriesQuickstart method createContainerIfNotExists.

// Container create
private void createContainerIfNotExists() throws Exception {
    logger.info("Create container " + containerName + " if not exists.");
    // Create container if not exists
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/lastName");
    // Provision throughput
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    // Create container with 200 RU/s
    CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties, throughputProperties);
    container = database.getContainer(containerResponse.getProperties().getId());
    logger.info("Done.");
}
Also used : ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) 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