Search in sources :

Example 1 with CosmosPagedFlux

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

the class SampleStoredProcedureAsync method readAllSprocs.

private void readAllSprocs() throws Exception {
    CosmosPagedFlux<CosmosStoredProcedureProperties> fluxResponse = container.getScripts().readAllStoredProcedures();
    final CountDownLatch completionLatch = new CountDownLatch(1);
    fluxResponse.flatMap(storedProcedureProperties -> {
        logger.info(String.format("Stored Procedure: %s\n", storedProcedureProperties.getId()));
        return Mono.empty();
    }).subscribe(s -> {
    }, 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();
    });
    completionLatch.await();
}
Also used : CustomPOJO(com.azure.cosmos.examples.common.CustomPOJO) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) Logger(org.slf4j.Logger) PartitionKey(com.azure.cosmos.models.PartitionKey) LoggerFactory(org.slf4j.LoggerFactory) Mono(reactor.core.publisher.Mono) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) CosmosException(com.azure.cosmos.CosmosException) CosmosPagedFlux(com.azure.cosmos.util.CosmosPagedFlux) AccountSettings(com.azure.cosmos.examples.common.AccountSettings) CosmosException(com.azure.cosmos.CosmosException) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with CosmosPagedFlux

use of com.azure.cosmos.util.CosmosPagedFlux in project kafka-connect-cosmosdb by microsoft.

the class CosmosDBSourceTaskTest method setup.

@Before
// Need to maintain Typed objects
@SuppressWarnings("unchecked")
public void setup() throws IllegalAccessException {
    testTask = new CosmosDBSourceTask();
    // Configure settings
    sourceSettings = CosmosDBSourceConfigTest.setupConfigs();
    sourceSettings.put(CosmosDBSourceConfig.COSMOS_CONTAINER_TOPIC_MAP_CONF, topicName + "#" + containerName);
    sourceSettings.put(CosmosDBSourceConfig.COSMOS_DATABASE_NAME_CONF, databaseName);
    sourceSettings.put(CosmosDBSourceConfig.COSMOS_ASSIGNED_CONTAINER_CONF, containerName);
    sourceSettings.put(CosmosDBSourceConfig.COSMOS_SOURCE_TASK_POLL_INTERVAL_CONF, "500");
    sourceSettings.put(CosmosDBSourceConfig.COSMOS_SOURCE_TASK_BATCH_SIZE_CONF, "1");
    sourceSettings.put(CosmosDBSourceConfig.COSMOS_SOURCE_TASK_BUFFER_SIZE_CONF, "5000");
    sourceSettings.put(CosmosDBSourceConfig.COSMOS_SOURCE_TASK_TIMEOUT_CONF, "1000");
    config = new CosmosDBSourceConfig(sourceSettings);
    FieldUtils.writeField(testTask, "config", config, true);
    // Create the TransferQueue
    LinkedTransferQueue<JsonNode> queue = new LinkedTransferQueue<>();
    FieldUtils.writeField(testTask, "queue", queue, true);
    // Set the running flag to true
    AtomicBoolean running = new AtomicBoolean();
    running.set(true);
    FieldUtils.writeField(testTask, "running", running, true);
    // Mock the Cosmos SDK
    mockCosmosClient = Mockito.mock(CosmosAsyncClient.class);
    CosmosAsyncDatabase mockDatabase = Mockito.mock(CosmosAsyncDatabase.class);
    when(mockCosmosClient.getDatabase(anyString())).thenReturn(mockDatabase);
    mockFeedContainer = Mockito.mock(CosmosAsyncContainer.class);
    when(mockDatabase.getContainer("feed")).thenReturn(mockFeedContainer);
    mockLeaseContainer = Mockito.mock(CosmosAsyncContainer.class);
    when(mockDatabase.getContainer("lease")).thenReturn(mockLeaseContainer);
    // Mock query results and iterator for getting lease container token
    CosmosPagedFlux<JsonNode> mockLeaseQueryResults = (CosmosPagedFlux<JsonNode>) Mockito.mock(CosmosPagedFlux.class);
    when(mockLeaseContainer.queryItems(anyString(), any(), eq(JsonNode.class))).thenReturn(mockLeaseQueryResults);
    Iterable<JsonNode> mockLeaseQueryIterable = (Iterable<JsonNode>) Mockito.mock(Iterable.class);
    when(mockLeaseQueryResults.toIterable()).thenReturn(mockLeaseQueryIterable);
    Iterator<JsonNode> mockLeaseQueryIterator = (Iterator<JsonNode>) Mockito.mock(Iterator.class);
    when(mockLeaseQueryIterable.iterator()).thenReturn(mockLeaseQueryIterator);
    when(mockLeaseQueryIterator.hasNext()).thenReturn(false);
    FieldUtils.writeField(testTask, "client", mockCosmosClient, true);
    FieldUtils.writeField(testTask, "leaseContainer", mockLeaseContainer, true);
}
Also used : CosmosPagedFlux(com.azure.cosmos.util.CosmosPagedFlux) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) JsonNode(com.fasterxml.jackson.databind.JsonNode) LinkedTransferQueue(java.util.concurrent.LinkedTransferQueue) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) Iterator(java.util.Iterator) Before(org.junit.Before)

Example 3 with CosmosPagedFlux

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

the class SampleCRUDQuickstartAsync method queryItems.

private void queryItems() {
    // <QueryItems>
    // Set some common query options
    // We'll use this later
    int preferredPageSize = 10;
    CosmosQueryRequestOptions queryOptions = new CosmosQueryRequestOptions();
    // Set populate query metrics to get metrics around query executions
    queryOptions.setQueryMetricsEnabled(true);
    CosmosPagedFlux<Family> pagedFluxResponse = container.queryItems("SELECT * FROM Family WHERE Family.lastName IN ('Andersen', 'Wakefield', 'Johnson')", queryOptions, Family.class);
    try {
        pagedFluxResponse.byPage(preferredPageSize).flatMap(fluxResponse -> {
            logger.info("Got a page of query result with " + fluxResponse.getResults().size() + " items(s)" + " and request charge of " + fluxResponse.getRequestCharge());
            logger.info("Item Ids " + fluxResponse.getResults().stream().map(Family::getId).collect(Collectors.toList()));
            return Flux.empty();
        }).blockLast();
    } catch (Exception err) {
        if (err instanceof CosmosException) {
            // Client-specific errors
            CosmosException cerr = (CosmosException) err;
            cerr.printStackTrace();
            logger.error(String.format("Read Item failed with %s\n", cerr));
        } else {
            // General errors
            err.printStackTrace();
        }
    }
// </QueryItems>
}
Also used : CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) PartitionKey(com.azure.cosmos.models.PartitionKey) LoggerFactory(org.slf4j.LoggerFactory) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) ArrayList(java.util.ArrayList) CosmosContainerRequestOptions(com.azure.cosmos.models.CosmosContainerRequestOptions) 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) Logger(org.slf4j.Logger) 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) 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) Family(com.azure.cosmos.examples.common.Family) CosmosException(com.azure.cosmos.CosmosException) CosmosException(com.azure.cosmos.CosmosException)

Example 4 with CosmosPagedFlux

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

the class CosmosDiagnosticsQuickStartAsync method queryDocuments.

private void queryDocuments() throws Exception {
    logger.info("Query documents in the container : {}", containerName);
    String sql = "SELECT * FROM c WHERE c.lastName = 'Witherspoon'";
    CosmosPagedFlux<Family> filteredFamilies = container.queryItems(sql, new CosmosQueryRequestOptions(), Family.class);
    // Add handler to capture diagnostics
    filteredFamilies = filteredFamilies.handle(familyFeedResponse -> {
        logger.info("Query Item diagnostics through handler : {}", familyFeedResponse.getCosmosDiagnostics());
    });
    // Or capture diagnostics through byPage() APIs.
    filteredFamilies.byPage().toIterable().forEach(familyFeedResponse -> {
        logger.info("Query item diagnostics through iterableByPage : {}", familyFeedResponse.getCosmosDiagnostics());
    });
    logger.info("Done.");
}
Also used : CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) Logger(org.slf4j.Logger) CosmosDatabaseRequestOptions(com.azure.cosmos.models.CosmosDatabaseRequestOptions) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) PartitionKey(com.azure.cosmos.models.PartitionKey) LoggerFactory(org.slf4j.LoggerFactory) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) Family(com.azure.cosmos.examples.common.Family) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) CosmosException(com.azure.cosmos.CosmosException) CosmosPagedFlux(com.azure.cosmos.util.CosmosPagedFlux) CosmosDiagnostics(com.azure.cosmos.CosmosDiagnostics) AccountSettings(com.azure.cosmos.examples.common.AccountSettings) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) Family(com.azure.cosmos.examples.common.Family)

Example 5 with CosmosPagedFlux

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

the class SampleSubpartitioningAsync method queryItems.

// Add the
private void queryItems() {
    // <QueryItems>
    // Set some common query options
    // We'll use this later
    int preferredPageSize = 10;
    CosmosQueryRequestOptions queryOptions = new CosmosQueryRequestOptions();
    // Set populate query metrics to get metrics around query executions
    queryOptions.setQueryMetricsEnabled(true);
    /**
     *  Subpartitioning support:
     *      Specifying partial partition key values in query, will only route the query to
     *      the subset of physical partitions on which the documents with this partition key
     *      value exist.
     */
    CosmosPagedFlux<UserSession> pagedFluxResponse = container.queryItems("SELECT * FROM t WHERE t.TenantId IN ('Microsoft')", queryOptions, UserSession.class);
    try {
        pagedFluxResponse.byPage(preferredPageSize).flatMap(fluxResponse -> {
            logger.info("Got a page of query result with " + fluxResponse.getResults().size() + " items(s)" + " and request charge of " + fluxResponse.getRequestCharge());
            logger.info("Item Ids " + fluxResponse.getResults().stream().map(UserSession::getId).collect(Collectors.toList()));
            return Flux.empty();
        }).blockLast();
    } catch (Exception err) {
        if (err instanceof CosmosException) {
            // Client-specific errors
            CosmosException cerr = (CosmosException) err;
            cerr.printStackTrace();
            logger.error(String.format("Read Item failed with %s\n", cerr));
        } else {
            // General errors
            err.printStackTrace();
        }
    }
// </QueryItems>
}
Also used : CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) PartitionKey(com.azure.cosmos.models.PartitionKey) LoggerFactory(org.slf4j.LoggerFactory) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) SampleCRUDQuickstartAsync(com.azure.cosmos.examples.crudquickstart.async.SampleCRUDQuickstartAsync) ArrayList(java.util.ArrayList) CosmosContainerRequestOptions(com.azure.cosmos.models.CosmosContainerRequestOptions) 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) PartitionKind(com.azure.cosmos.models.PartitionKind) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) Logger(org.slf4j.Logger) UserSessionData(com.azure.cosmos.examples.common.UserSessionData) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) PartitionKeyDefinitionVersion(com.azure.cosmos.models.PartitionKeyDefinitionVersion) Mono(reactor.core.publisher.Mono) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) Collectors(java.util.stream.Collectors) PartitionKeyBuilder(com.azure.cosmos.models.PartitionKeyBuilder) UserSession(com.azure.cosmos.examples.common.UserSession) Flux(reactor.core.publisher.Flux) List(java.util.List) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) PartitionKeyDefinition(com.azure.cosmos.models.PartitionKeyDefinition) CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse) CosmosPagedFlux(com.azure.cosmos.util.CosmosPagedFlux) AccountSettings(com.azure.cosmos.examples.common.AccountSettings) UserSession(com.azure.cosmos.examples.common.UserSession) CosmosException(com.azure.cosmos.CosmosException) CosmosException(com.azure.cosmos.CosmosException)

Aggregations

CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)5 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)5 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)5 CosmosPagedFlux (com.azure.cosmos.util.CosmosPagedFlux)5 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)4 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)4 CosmosException (com.azure.cosmos.CosmosException)4 AccountSettings (com.azure.cosmos.examples.common.AccountSettings)4 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)4 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)4 PartitionKey (com.azure.cosmos.models.PartitionKey)4 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 Mono (reactor.core.publisher.Mono)4 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)3 CosmosDatabaseResponse (com.azure.cosmos.models.CosmosDatabaseResponse)3 CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)3 ArrayList (java.util.ArrayList)3 Family (com.azure.cosmos.examples.common.Family)2