Search in sources :

Example 1 with CosmosAsyncDatabase

use of com.azure.cosmos.CosmosAsyncDatabase 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 CosmosAsyncDatabase

use of com.azure.cosmos.CosmosAsyncDatabase 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 CosmosAsyncDatabase

use of com.azure.cosmos.CosmosAsyncDatabase 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 4 with CosmosAsyncDatabase

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

the class SalesOrder method MigrateJavaSDKv4ItemOperationsAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/migrate-java-v4-sdk
 * Item operations
 */
/**
 * Item operations
 */
public static void MigrateJavaSDKv4ItemOperationsAsync() {
    String container_id = "family_container";
    String partition_key = "/pk";
    CosmosAsyncDatabase database = null;
    // <MigrateItemOpsAsync>
    // Container is created. Generate many docs to insert.
    int number_of_docs = 50000;
    ArrayList<JsonNode> docs = generateManyDocs(number_of_docs);
    // Insert many docs into container...
    Flux.fromIterable(docs).flatMap(doc -> testContainerAsync.createItem(doc)).subscribe();
// </MigrateItemOpsAsync>
}
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) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 5 with CosmosAsyncDatabase

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

Aggregations

CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)12 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)10 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)8 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)7 ConflictResolutionPolicy (com.azure.cosmos.models.ConflictResolutionPolicy)5 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ChangeFeedProcessor (com.azure.cosmos.ChangeFeedProcessor)3 ChangeFeedProcessorBuilder (com.azure.cosmos.ChangeFeedProcessorBuilder)3 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)3 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)3 CosmosException (com.azure.cosmos.CosmosException)3 CosmosContainerRequestOptions (com.azure.cosmos.models.CosmosContainerRequestOptions)3 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)3 ExcludedPath (com.azure.cosmos.models.ExcludedPath)3 IncludedPath (com.azure.cosmos.models.IncludedPath)3 IndexingPolicy (com.azure.cosmos.models.IndexingPolicy)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Duration (java.time.Duration)3