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>
}
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));
}
Aggregations