Search in sources :

Example 1 with CosmosItemResponse

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

the class SampleIndexManagementAsync method readItems.

private void readItems(Flux<Family> familiesToCreate) {
    // Using partition key for point read scenarios.
    // This will help fast look up of items because of partition key
    // <ReadItem>
    final CountDownLatch completionLatch = new CountDownLatch(1);
    familiesToCreate.flatMap(family -> {
        Mono<CosmosItemResponse<Family>> asyncItemResponseMono = container.readItem(family.getId(), new PartitionKey(family.getLastName()), Family.class);
        return asyncItemResponseMono;
    }).subscribe(itemResponse -> {
        double requestCharge = itemResponse.getRequestCharge();
        Duration requestLatency = itemResponse.getDuration();
        logger.info(String.format("Item successfully read with id %s with a charge of %.2f and within duration %s", itemResponse.getItem().getId(), requestCharge, requestLatency));
    }, err -> {
        if (err instanceof CosmosException) {
            // Client-specific errors
            CosmosException cerr = (CosmosException) err;
            cerr.printStackTrace();
            logger.info(String.format("Read Item failed with %s\n", cerr));
        } else {
            // General errors
            err.printStackTrace();
        }
        completionLatch.countDown();
    }, () -> {
        completionLatch.countDown();
    });
    try {
        completionLatch.await();
    } catch (InterruptedException err) {
        throw new AssertionError("Unexpected Interruption", err);
    }
// </ReadItem>
}
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) Mono(reactor.core.publisher.Mono) Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey) Duration(java.time.Duration) CosmosException(com.azure.cosmos.CosmosException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with CosmosItemResponse

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

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

Example 4 with CosmosItemResponse

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

the class CosmosDiagnosticsQuickStartAsync method replaceDocument.

private void replaceDocument() throws Exception {
    logger.info("Replace document : {}", documentId);
    // Replace existing document with new modified document
    Family family = new Family();
    family.setLastName(documentLastName);
    family.setId(documentId);
    // Document modification
    family.setDistrict("Columbia");
    Mono<CosmosItemResponse<Family>> itemResponseMono = container.replaceItem(family, family.getId(), new PartitionKey(family.getLastName()), new CosmosItemRequestOptions());
    CosmosItemResponse<Family> itemResponse = itemResponseMono.block();
    CosmosDiagnostics diagnostics = itemResponse.getDiagnostics();
    logger.info("Replace item diagnostics : {}", diagnostics);
    logger.info("Request charge of replace operation: {} RU", itemResponse.getRequestCharge());
    logger.info("Done.");
}
Also used : CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosDiagnostics(com.azure.cosmos.CosmosDiagnostics)

Example 5 with CosmosItemResponse

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

the class CosmosDiagnosticsQuickStartAsync method upsertDocument.

private void upsertDocument() throws Exception {
    logger.info("Replace document : {}", documentId);
    // Replace existing document with new modified document (contingent on modification).
    Family family = new Family();
    family.setLastName(documentLastName);
    family.setId(documentId);
    // Document modification
    family.setDistrict("Columbia");
    Mono<CosmosItemResponse<Family>> itemResponseMono = container.upsertItem(family, new CosmosItemRequestOptions());
    CosmosItemResponse<Family> itemResponse = itemResponseMono.block();
    CosmosDiagnostics diagnostics = itemResponse.getDiagnostics();
    logger.info("Upsert item diagnostics : {}", diagnostics);
    logger.info("Done.");
}
Also used : CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) Family(com.azure.cosmos.examples.common.Family) CosmosDiagnostics(com.azure.cosmos.CosmosDiagnostics)

Aggregations

CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)9 CosmosDiagnostics (com.azure.cosmos.CosmosDiagnostics)5 Family (com.azure.cosmos.examples.common.Family)5 PartitionKey (com.azure.cosmos.models.PartitionKey)5 CosmosItemRequestOptions (com.azure.cosmos.models.CosmosItemRequestOptions)4 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)3 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)2 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)2 AccountSettings (com.azure.cosmos.examples.common.AccountSettings)2 CustomPOJO (com.azure.cosmos.examples.common.CustomPOJO)2 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)2 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)2 CosmosDatabaseResponse (com.azure.cosmos.models.CosmosDatabaseResponse)2 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Flux (reactor.core.publisher.Flux)2 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)1