use of com.azure.cosmos.examples.common.Family in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class DocumentCRUDQuickstart method readDocumentOnlyIfChanged.
private void readDocumentOnlyIfChanged() throws Exception {
logger.info("Read document " + documentId + " only if it has been changed, utilizing an ETag check.");
// Read document
CosmosItemResponse<Family> famResp = container.readItem(documentId, new PartitionKey(documentLastName), Family.class);
logger.info("Read doc with status code of {}", famResp.getStatusCode());
// Re-read but with conditional access requirement that ETag has changed.
// This should fail.
String etag = famResp.getResponseHeaders().get("etag");
CosmosItemRequestOptions requestOptions = new CosmosItemRequestOptions();
requestOptions.setIfNoneMatchETag(etag);
CosmosItemResponse<Family> failResp = container.readItem(documentId, new PartitionKey(documentLastName), requestOptions, Family.class);
logger.info("Re-read doc with status code of {} (we anticipate failure due to ETag not having changed.)", failResp.getStatusCode());
// Replace the doc with a modified version, which will update ETag
Family family = famResp.getItem();
family.setRegistered(!family.isRegistered());
CosmosItemResponse<Family> failedFamResp = container.replaceItem(family, family.getId(), new PartitionKey(family.getLastName()), new CosmosItemRequestOptions());
logger.info("Modified and replaced the doc (updates ETag.)");
// Re-read doc again, with conditional acccess requirements.
// This should succeed since ETag has been updated.
CosmosItemResponse<Family> succeedResp = container.readItem(documentId, new PartitionKey(documentLastName), requestOptions, Family.class);
logger.info("Re-read doc with status code of {} (we anticipate success due to ETag modification.)", succeedResp.getStatusCode());
logger.info("Done.");
}
use of com.azure.cosmos.examples.common.Family in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class DocumentCRUDQuickstart method replaceDocument.
private void replaceDocument() throws Exception {
logger.info("Replace document " + documentId);
// Replace existing document with new modified document
Family family = new Family();
family.setLastName(documentLastName);
family.setId(documentId);
// Document modification
family.setDistrict("Columbia");
CosmosItemResponse<Family> famResp = container.replaceItem(family, family.getId(), new PartitionKey(family.getLastName()), new CosmosItemRequestOptions());
logger.info("Request charge of replace operation: {} RU", famResp.getRequestCharge());
logger.info("Done.");
}
use of com.azure.cosmos.examples.common.Family in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class DocumentCRUDQuickstart method upsertDocument.
private void upsertDocument() throws Exception {
logger.info("Replace document " + documentId);
// Replace existing document with new modified document (contingent on modification).
Family family = new Family();
family.setLastName(documentLastName);
family.setId(documentId);
// Document modification
family.setDistrict("Columbia");
CosmosItemResponse<Family> famResp = container.upsertItem(family, new CosmosItemRequestOptions());
logger.info("Done.");
}
use of com.azure.cosmos.examples.common.Family in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleIndexManagementAsync method queryItems.
private void queryItems() {
// <QueryItems>
// Set some common query options
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);
final CountDownLatch completionLatch = new CountDownLatch(1);
pagedFluxResponse.byPage(preferredPageSize).subscribe(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()));
}, 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();
}
completionLatch.countDown();
}, () -> {
completionLatch.countDown();
});
try {
completionLatch.await();
} catch (InterruptedException err) {
throw new AssertionError("Unexpected Interruption", err);
}
// </QueryItems>
}
use of com.azure.cosmos.examples.common.Family 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>
}
Aggregations