Search in sources :

Example 1 with UserSession

use of com.azure.cosmos.examples.common.UserSession 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)

Example 2 with UserSession

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

the class SampleSubpartitioningAsync method getStartedDemo.

// </Main>
private void getStartedDemo() throws Exception {
    logger.info("Using Azure Cosmos DB endpoint: " + AccountSettings.HOST);
    ArrayList<String> preferredRegions = new ArrayList<String>();
    preferredRegions.add("West US");
    // Setting the preferred location to Cosmos DB Account region
    // West US is just an example. User should set preferred location to the Cosmos DB region closest to the application
    // Create async client
    // <CreateAsyncClient>
    client = new CosmosClientBuilder().endpoint(AccountSettings.HOST).key(AccountSettings.MASTER_KEY).preferredRegions(preferredRegions).contentResponseOnWriteEnabled(true).consistencyLevel(ConsistencyLevel.SESSION).buildAsyncClient();
    // </CreateAsyncClient>
    createDatabaseIfNotExists();
    createContainerIfNotExists();
    // Setup UserSession items to create
    List<UserSession> userSessions = UserSessionData.buildSampleSessionData();
    Flux<UserSession> familiesToCreate = Flux.fromIterable(userSessions);
    // Creates several items in the container
    createFamilies(familiesToCreate);
    logger.info("Reading items.");
    readItems(familiesToCreate);
    logger.info("Querying items.");
    queryItems();
    logger.info("Deleting an item.");
    deleteItem(userSessions.get(0));
}
Also used : CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) UserSession(com.azure.cosmos.examples.common.UserSession) ArrayList(java.util.ArrayList)

Aggregations

CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)2 UserSession (com.azure.cosmos.examples.common.UserSession)2 ArrayList (java.util.ArrayList)2 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)1 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)1 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)1 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)1 CosmosException (com.azure.cosmos.CosmosException)1 AccountSettings (com.azure.cosmos.examples.common.AccountSettings)1 UserSessionData (com.azure.cosmos.examples.common.UserSessionData)1 SampleCRUDQuickstartAsync (com.azure.cosmos.examples.crudquickstart.async.SampleCRUDQuickstartAsync)1 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)1 CosmosContainerRequestOptions (com.azure.cosmos.models.CosmosContainerRequestOptions)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 PartitionKey (com.azure.cosmos.models.PartitionKey)1 PartitionKeyBuilder (com.azure.cosmos.models.PartitionKeyBuilder)1 PartitionKeyDefinition (com.azure.cosmos.models.PartitionKeyDefinition)1