Search in sources :

Example 1 with CosmosAsyncContainer

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

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

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

the class SalesOrder method TroubleshootingGuideJavaSDKv4PublishOnSchedulerAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/troubleshoot-java-sdk-v4-sql
 * Troubleshooting guide - publish on scheduler
 * Async only
 */
/**
 * Troubleshooting guide - publish on scheduler
 */
public static void TroubleshootingGuideJavaSDKv4PublishOnSchedulerAsync() {
    CosmosAsyncContainer container = null;
    Scheduler customScheduler = null;
    Family family = null;
    // <TroubleshootPublishOnSchedulerAsync>
    container.createItem(family).publishOn(// Switches the thread.
    customScheduler).subscribe();
// </TroubleshootPublishOnSchedulerAsync>
}
Also used : CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) Scheduler(reactor.core.scheduler.Scheduler) Family(com.azure.cosmos.examples.common.Family)

Example 4 with CosmosAsyncContainer

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

the class SalesOrder method PerformanceTipsJavaSDKv4NeedsSchedulerAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips-java-sdk-v4-sql
 * Performance tips - needs scheduler
 * Async only
 */
/**
 * Performance tips - needs scheduler
 */
public static void PerformanceTipsJavaSDKv4NeedsSchedulerAsync() {
    CosmosAsyncContainer asyncContainer = null;
    CustomPOJO item = null;
    // <PerformanceNeedsSchedulerAsync>
    Mono<CosmosItemResponse<CustomPOJO>> createItemPub = asyncContainer.createItem(item);
    createItemPub.subscribe(itemResponse -> {
        // this is executed on eventloop IO netty thread.
        // the eventloop thread is shared and is meant to return back quickly.
        // 
        // DON'T do this on eventloop IO netty thread.
        veryCpuIntensiveWork();
    });
// </PerformanceNeedsSchedulerAsync>
}
Also used : CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CustomPOJO(com.azure.cosmos.examples.common.CustomPOJO)

Example 5 with CosmosAsyncContainer

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

the class SalesOrder method PerformanceTipsJavaSDKv4AddSchedulerSync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips-java-sdk-v4-sql
 * Performance tips - add scheduler
 * Async only
 */
/**
 * Performance tips - add scheduler
 */
public static void PerformanceTipsJavaSDKv4AddSchedulerSync() {
    CosmosAsyncContainer asyncContainer = null;
    CustomPOJO item = null;
    // <PerformanceAddSchedulerAsync>
    Mono<CosmosItemResponse<CustomPOJO>> createItemPub = asyncContainer.createItem(item);
    createItemPub.subscribeOn(Schedulers.elastic()).subscribe(itemResponse -> {
        // this is executed on eventloop IO netty thread.
        // the eventloop thread is shared and is meant to return back quickly.
        // 
        // DON'T do this on eventloop IO netty thread.
        veryCpuIntensiveWork();
    });
// </PerformanceAddSchedulerAsync>
}
Also used : CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CustomPOJO(com.azure.cosmos.examples.common.CustomPOJO)

Aggregations

CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)16 CustomPOJO (com.azure.cosmos.examples.common.CustomPOJO)9 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)7 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)7 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 ChangeFeedProcessor (com.azure.cosmos.ChangeFeedProcessor)4 ChangeFeedProcessorBuilder (com.azure.cosmos.ChangeFeedProcessorBuilder)4 CosmosItemRequestOptions (com.azure.cosmos.models.CosmosItemRequestOptions)4 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)4 PartitionKey (com.azure.cosmos.models.PartitionKey)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 Scheduler (reactor.core.scheduler.Scheduler)4 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)3 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)3 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)3 Family (com.azure.cosmos.examples.common.Family)3 ExcludedPath (com.azure.cosmos.models.ExcludedPath)3 IncludedPath (com.azure.cosmos.models.IncludedPath)3 IndexingPolicy (com.azure.cosmos.models.IndexingPolicy)3