use of com.azure.cosmos.examples.common.Family 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>
}
use of com.azure.cosmos.examples.common.Family in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleIndexManagementAsync method indexManagementDemo.
// </Main>
private void indexManagementDemo() throws Exception {
logger.info("Using Azure Cosmos DB endpoint: " + AccountSettings.HOST);
ArrayList<String> preferredRegions = new ArrayList<String>();
preferredRegions.add("West US");
// Create async client
// <CreateAsyncClient>
client = new CosmosClientBuilder().endpoint(AccountSettings.HOST).key(AccountSettings.MASTER_KEY).preferredRegions(preferredRegions).consistencyLevel(ConsistencyLevel.EVENTUAL).contentResponseOnWriteEnabled(true).buildAsyncClient();
// </CreateAsyncClient>
createDatabaseIfNotExists();
// Here is where index management is performed
createContainerIfNotExistsWithSpecifiedIndex();
Family andersenFamilyItem = Families.getAndersenFamilyItem();
Family wakefieldFamilyItem = Families.getWakefieldFamilyItem();
Family johnsonFamilyItem = Families.getJohnsonFamilyItem();
Family smithFamilyItem = Families.getSmithFamilyItem();
// Setup family items to create
Flux<Family> familiesToCreate = Flux.just(andersenFamilyItem, wakefieldFamilyItem, johnsonFamilyItem, smithFamilyItem);
createFamilies(familiesToCreate);
familiesToCreate = Flux.just(andersenFamilyItem, wakefieldFamilyItem, johnsonFamilyItem, smithFamilyItem);
logger.info("Reading items.");
readItems(familiesToCreate);
logger.info("Querying items.");
queryItems();
}
use of com.azure.cosmos.examples.common.Family in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleIndexManagement method createFamilies.
private void createFamilies(List<Family> families) throws Exception {
double totalRequestCharge = 0;
for (Family family : families) {
// <CreateItem>
// Create item using container that we created using sync client
// Use lastName as partitionKey for cosmos item
// Using appropriate partition key improves the performance of database operations
CosmosItemRequestOptions cosmosItemRequestOptions = new CosmosItemRequestOptions();
CosmosItemResponse<Family> item = container.createItem(family, new PartitionKey(family.getLastName()), cosmosItemRequestOptions);
// </CreateItem>
// Get request charge and other properties like latency, and diagnostics strings, etc.
logger.info(String.format("Created item with request charge of %.2f within" + " duration %s", item.getRequestCharge(), item.getDuration()));
totalRequestCharge += item.getRequestCharge();
}
logger.info(String.format("Created %d items with total request " + "charge of %.2f", families.size(), totalRequestCharge));
}
use of com.azure.cosmos.examples.common.Family in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class CosmosDiagnosticsQuickStart method queryDocuments.
private void queryDocuments() throws Exception {
logger.info("Query documents in the container : {}", containerName);
String sql = "SELECT * FROM c WHERE c.lastName = 'Witherspoon'";
CosmosPagedIterable<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 iterableByPage() APIs.
filteredFamilies.iterableByPage().forEach(familyFeedResponse -> {
logger.info("Query item diagnostics through iterableByPage : {}", familyFeedResponse.getCosmosDiagnostics());
});
logger.info("Done.");
}
use of com.azure.cosmos.examples.common.Family in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class CosmosDiagnosticsQuickStart method readDocumentDoesntExist.
// Document read doesn't exist
private void readDocumentDoesntExist() throws Exception {
logger.info("Read document by ID : bad-ID");
// Read document by ID
try {
CosmosItemResponse<Family> familyCosmosItemResponse = container.readItem("bad-ID", new PartitionKey("bad-lastName"), Family.class);
} catch (CosmosException cosmosException) {
CosmosDiagnostics diagnostics = cosmosException.getDiagnostics();
logger.info("Read item exception diagnostics : {}", diagnostics);
}
logger.info("Done.");
}
Aggregations