Search in sources :

Example 1 with Families

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

the class SampleIndexManagementAsync method createFamilies.

private void createFamilies(Flux<Family> families) throws Exception {
    // <CreateItem>
    final CountDownLatch completionLatch = new CountDownLatch(1);
    // Combine multiple item inserts, associated success println's, and a final aggregate stats println into one Reactive stream.
    families.flatMap(family -> {
        return container.createItem(family);
    }).flatMap(itemResponse -> {
        logger.info(String.format("Created item with request charge of %.2f within" + " duration %s", itemResponse.getRequestCharge(), itemResponse.getDuration()));
        logger.info(String.format("Item ID: %s\n", itemResponse.getItem().getId()));
        return Mono.just(itemResponse.getRequestCharge());
    }).reduce(0.0, (charge_n, charge_nplus1) -> charge_n + charge_nplus1).subscribe(charge -> {
        logger.info(String.format("Created items with total request charge of %.2f\n", charge));
    }, 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);
    }
// </CreateItem>
}
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) CosmosException(com.azure.cosmos.CosmosException) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)1 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)1 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)1 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)1 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)1 CosmosException (com.azure.cosmos.CosmosException)1 AccountSettings (com.azure.cosmos.examples.common.AccountSettings)1 Families (com.azure.cosmos.examples.common.Families)1 Family (com.azure.cosmos.examples.common.Family)1 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)1 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)1 CosmosDatabaseResponse (com.azure.cosmos.models.CosmosDatabaseResponse)1 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)1 CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)1 ExcludedPath (com.azure.cosmos.models.ExcludedPath)1 IncludedPath (com.azure.cosmos.models.IncludedPath)1 IndexingMode (com.azure.cosmos.models.IndexingMode)1 IndexingPolicy (com.azure.cosmos.models.IndexingPolicy)1 PartitionKey (com.azure.cosmos.models.PartitionKey)1 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)1