Search in sources :

Example 6 with CosmosContainerProperties

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

the class CosmosDiagnosticsQuickStart 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
    CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties, throughputProperties);
    CosmosDiagnostics diagnostics = containerResponse.getDiagnostics();
    logger.info("Create container diagnostics : {}", diagnostics);
    container = database.getContainer(containerResponse.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 7 with CosmosContainerProperties

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

the class SalesOrder method ManageConflictResolutionPoliciesInAzureCosmosDBCustomAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/how-to-manage-conflicts
 * Resolve conflicts, stored procedure
 */
/**
 * Client-side conflict resolution with fully custom policy
 */
public static void ManageConflictResolutionPoliciesInAzureCosmosDBCustomAsync() {
    String container_id = "family_container";
    String partition_key = "/pk";
    CosmosAsyncDatabase database = null;
    // <ManageConflictResolutionCustomAsync>
    ConflictResolutionPolicy policy = ConflictResolutionPolicy.createCustomPolicy();
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(container_id, partition_key);
    containerProperties.setConflictResolutionPolicy(policy);
    /* ...other container config... */
    database.createContainerIfNotExists(containerProperties).block();
// </ManageConflictResolutionCustomAsync>
}
Also used : CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) ConflictResolutionPolicy(com.azure.cosmos.models.ConflictResolutionPolicy)

Example 8 with CosmosContainerProperties

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

the class SalesOrder method ManageConflictResolutionPoliciesInAzureCosmosDBLWWAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/how-to-manage-conflicts
 * Resolve conflicts, LWW policy
 */
/**
 * Client-side conflict resolution settings for LWW policy
 */
public static void ManageConflictResolutionPoliciesInAzureCosmosDBLWWAsync() {
    String container_id = "family_container";
    String partition_key = "/pk";
    CosmosAsyncDatabase database = null;
    // <ManageConflictResolutionLWWAsync>
    ConflictResolutionPolicy policy = ConflictResolutionPolicy.createLastWriterWinsPolicy("/myCustomId");
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(container_id, partition_key);
    containerProperties.setConflictResolutionPolicy(policy);
    /* ...other container config... */
    database.createContainerIfNotExists(containerProperties).block();
// </ManageConflictResolutionLWWAsync>
}
Also used : CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) ConflictResolutionPolicy(com.azure.cosmos.models.ConflictResolutionPolicy)

Example 9 with CosmosContainerProperties

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

the class SalesOrder method MigrateJavaSDKv4ResourceAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/migrate-java-v4-sdk
 * Migrate previous versions to Java SDK v4
 */
/**
 * Migrate previous versions to Java SDK v4
 */
public static void MigrateJavaSDKv4ResourceAsync() {
    String container_id = "family_container";
    String partition_key = "/pk";
    CosmosAsyncDatabase database = null;
    // <MigrateJavaSDKv4ResourceAsync>
    // Create Async client.
    // Building an async client is still a sync operation.
    CosmosAsyncClient client = new CosmosClientBuilder().endpoint("your.hostname").key("yourmasterkey").consistencyLevel(ConsistencyLevel.EVENTUAL).buildAsyncClient();
    // Create database with specified name
    client.createDatabaseIfNotExists("YourDatabaseName").flatMap(databaseResponse -> {
        testDatabaseAsync = client.getDatabase("YourDatabaseName");
        // Container properties - name and partition key
        CosmosContainerProperties containerProperties = new CosmosContainerProperties("YourContainerName", "/id");
        // Provision manual throughput
        ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
        // Create container
        return database.createContainerIfNotExists(containerProperties, throughputProperties);
    }).flatMap(containerResponse -> {
        testContainerAsync = database.getContainer("YourContainerName");
        return Mono.empty();
    }).subscribe();
// </MigrateJavaSDKv4ResourceAsync>
}
Also used : PartitionKey(com.azure.cosmos.models.PartitionKey) LoggerFactory(org.slf4j.LoggerFactory) ExcludedPath(com.azure.cosmos.models.ExcludedPath) Scheduler(reactor.core.scheduler.Scheduler) ArrayList(java.util.ArrayList) ProxyOptions(com.azure.core.http.ProxyOptions) DirectConnectionConfig(com.azure.cosmos.DirectConnectionConfig) Families(com.azure.cosmos.examples.common.Families) Family(com.azure.cosmos.examples.common.Family) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) Duration(java.time.Duration) Schedulers(reactor.core.scheduler.Schedulers) ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) JsonNode(com.fasterxml.jackson.databind.JsonNode) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) ExecutorService(java.util.concurrent.ExecutorService) ChangeFeedProcessorBuilder(com.azure.cosmos.ChangeFeedProcessorBuilder) CustomPOJO(com.azure.cosmos.examples.common.CustomPOJO) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy) Logger(org.slf4j.Logger) GatewayConnectionConfig(com.azure.cosmos.GatewayConnectionConfig) IncludedPath(com.azure.cosmos.models.IncludedPath) IndexingMode(com.azure.cosmos.models.IndexingMode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Mono(reactor.core.publisher.Mono) ChangeFeedProcessor(com.azure.cosmos.ChangeFeedProcessor) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) List(java.util.List) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) ConflictResolutionPolicy(com.azure.cosmos.models.ConflictResolutionPolicy) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties)

Example 10 with CosmosContainerProperties

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

the class SalesOrder method MigrateJavaSDKv4IndexingAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/migrate-java-v4-sdk
 * Indexing
 */
/**
 * Indexing
 */
public static void MigrateJavaSDKv4IndexingAsync() {
    String containerName = "family_container";
    String partition_key = "/pk";
    CosmosAsyncDatabase database = null;
    // <MigrateIndexingAsync>
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/lastName");
    // Custom indexing policy
    IndexingPolicy indexingPolicy = new IndexingPolicy();
    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);
    containerProperties.setIndexingPolicy(indexingPolicy);
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    database.createContainerIfNotExists(containerProperties, throughputProperties);
    CosmosAsyncContainer containerIfNotExists = database.getContainer(containerName);
// </MigrateIndexingAsync>
}
Also used : ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) 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)

Aggregations

CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)40 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)23 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)19 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)14 CosmosDatabase (com.azure.cosmos.CosmosDatabase)12 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)11 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)11 ArrayList (java.util.ArrayList)11 IndexingPolicy (com.azure.cosmos.models.IndexingPolicy)10 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 ConflictResolutionPolicy (com.azure.cosmos.models.ConflictResolutionPolicy)7 PartitionKey (com.azure.cosmos.models.PartitionKey)7 Mono (reactor.core.publisher.Mono)7 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)6 Flux (reactor.core.publisher.Flux)6