Search in sources :

Example 16 with CosmosDatabase

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

the class SampleDocumentationSnippets method ManageConflictResolutionPoliciesInAzureCosmosDBCustomSync.

/**
 * 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 ManageConflictResolutionPoliciesInAzureCosmosDBCustomSync() {
    String container_id = "family_container";
    String partition_key = "/pk";
    CosmosDatabase database = null;
    // <ManageConflictResolutionCustomSync>
    ConflictResolutionPolicy policy = ConflictResolutionPolicy.createCustomPolicy();
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(container_id, partition_key);
    containerProperties.setConflictResolutionPolicy(policy);
    /* ...other container config... */
    database.createContainerIfNotExists(containerProperties);
// </ManageConflictResolutionCustomSync>
}
Also used : CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) ConflictResolutionPolicy(com.azure.cosmos.models.ConflictResolutionPolicy)

Example 17 with CosmosDatabase

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

the class SampleDocumentationSnippets method ManageConflictResolutionPoliciesInAzureCosmosDBSprocSync.

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

Example 18 with CosmosDatabase

use of com.azure.cosmos.CosmosDatabase in project DataSpaceConnector by eclipse-dataspaceconnector.

the class CosmosDbApiImpl method getContainer.

private static CosmosContainer getContainer(Vault vault, AbstractCosmosConfig config) {
    CosmosClient client = createClient(vault, config.getAccountName(), Collections.singletonList(config.getPreferredRegion()));
    CosmosDatabase database = getDatabase(client, config.getDbName());
    if (database.readAllContainers().stream().noneMatch(sp -> sp.getId().equals(config.getContainerName()))) {
        throw new EdcException("No CosmosDB container named '" + config.getContainerName() + "' was found in account '" + config.getAccountName() + "'. Please create one, preferably using terraform.");
    }
    return database.getContainer(config.getContainerName());
}
Also used : CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosClient(com.azure.cosmos.CosmosClient) EdcException(org.eclipse.dataspaceconnector.spi.EdcException)

Aggregations

CosmosDatabase (com.azure.cosmos.CosmosDatabase)18 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)11 CosmosContainer (com.azure.cosmos.CosmosContainer)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 PartitionKey (com.azure.cosmos.models.PartitionKey)5 IndexingPolicy (com.azure.cosmos.models.IndexingPolicy)4 ExecutionException (com.scalar.db.exception.storage.ExecutionException)4 Before (org.junit.Before)4 CosmosClient (com.azure.cosmos.CosmosClient)3 ConflictResolutionPolicy (com.azure.cosmos.models.ConflictResolutionPolicy)3 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)3 CosmosItemRequestOptions (com.azure.cosmos.models.CosmosItemRequestOptions)3 CosmosStoredProcedureProperties (com.azure.cosmos.models.CosmosStoredProcedureProperties)3 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)2 CosmosScripts (com.azure.cosmos.CosmosScripts)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 TableMetadata (com.scalar.db.api.TableMetadata)2 URL (java.net.URL)2