Search in sources :

Example 16 with Family

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

the class CosmosDiagnosticsQuickStart 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> itemResponse = container.replaceItem(family, family.getId(), new PartitionKey(family.getLastName()), new CosmosItemRequestOptions());
    CosmosDiagnostics diagnostics = itemResponse.getDiagnostics();
    logger.info("Replace item diagnostics : {}", diagnostics);
    logger.info("Request charge of replace operation: {} RU", itemResponse.getRequestCharge());
    logger.info("Done.");
}
Also used : CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosDiagnostics(com.azure.cosmos.CosmosDiagnostics)

Example 17 with Family

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

the class CosmosDiagnosticsQuickStart 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> itemResponse = container.upsertItem(family, new CosmosItemRequestOptions());
    CosmosDiagnostics diagnostics = itemResponse.getDiagnostics();
    logger.info("Upsert item diagnostics : {}", diagnostics);
    logger.info("Done.");
}
Also used : CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) Family(com.azure.cosmos.examples.common.Family) CosmosDiagnostics(com.azure.cosmos.CosmosDiagnostics)

Example 18 with Family

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

the class SalesOrder method TroubleshootingGuideJavaSDKv4PublishOnSchedulerAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/troubleshoot-java-sdk-v4-sql
 * Troubleshooting guide - publish on scheduler
 * Async only
 */
/**
 * Troubleshooting guide - publish on scheduler
 */
public static void TroubleshootingGuideJavaSDKv4PublishOnSchedulerAsync() {
    CosmosAsyncContainer container = null;
    Scheduler customScheduler = null;
    Family family = null;
    // <TroubleshootPublishOnSchedulerAsync>
    container.createItem(family).publishOn(// Switches the thread.
    customScheduler).subscribe();
// </TroubleshootPublishOnSchedulerAsync>
}
Also used : CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) Scheduler(reactor.core.scheduler.Scheduler) Family(com.azure.cosmos.examples.common.Family)

Example 19 with Family

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

the class SampleIndexManagement method readItems.

private void readItems(ArrayList<Family> familiesToCreate) {
    // Using partition key for point read scenarios.
    // This will help fast look up of items because of partition key
    familiesToCreate.forEach(family -> {
        // <ReadItem>
        try {
            CosmosItemResponse<Family> item = container.readItem(family.getId(), new PartitionKey(family.getLastName()), Family.class);
            double requestCharge = item.getRequestCharge();
            Duration requestLatency = item.getDuration();
            logger.info(String.format("Item successfully read with id %s with a charge of %.2f and within duration %s", item.getItem().getId(), requestCharge, requestLatency));
        } catch (CosmosException e) {
            e.printStackTrace();
            logger.error(String.format("Read Item failed with %s", e));
        }
    // </ReadItem>
    });
}
Also used : Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey) Duration(java.time.Duration) CosmosException(com.azure.cosmos.CosmosException)

Example 20 with Family

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

the class SamplePatchQuickstart method patchAddSingleJsonString.

// demonstrates a single patch (add) operation using json String
private void patchAddSingleJsonString(String id, String partitionKey) {
    logger.info("Executing Patch with single 'add' operation to add json from String");
    String json = "[{\"givenName\": \"Goofy\"},{\"givenName\": \"Shadow\"}]";
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        // Converting json String to JsonNode using Jackson
        JsonNode jsonNode = objectMapper.readTree(json);
        CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
        // attribute does not exist, will be added
        cosmosPatchOperations.add("/pets", jsonNode);
        CosmosPatchItemRequestOptions options = new CosmosPatchItemRequestOptions();
        CosmosItemResponse<Family> response = this.container.patchItem(id, new PartitionKey(partitionKey), cosmosPatchOperations, options, Family.class);
        logger.info("Item with ID {} has been patched", response.getItem().getId());
    } catch (Exception e) {
        logger.error("failed", e);
    }
}
Also used : CosmosPatchItemRequestOptions(com.azure.cosmos.models.CosmosPatchItemRequestOptions) Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey) JsonNode(com.fasterxml.jackson.databind.JsonNode) CosmosPatchOperations(com.azure.cosmos.models.CosmosPatchOperations) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Family (com.azure.cosmos.examples.common.Family)58 PartitionKey (com.azure.cosmos.models.PartitionKey)37 CosmosItemRequestOptions (com.azure.cosmos.models.CosmosItemRequestOptions)19 CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)12 CosmosDiagnostics (com.azure.cosmos.CosmosDiagnostics)11 CosmosException (com.azure.cosmos.CosmosException)11 CosmosPatchOperations (com.azure.cosmos.models.CosmosPatchOperations)11 ArrayList (java.util.ArrayList)11 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)10 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)10 CosmosPatchItemRequestOptions (com.azure.cosmos.models.CosmosPatchItemRequestOptions)9 Duration (java.time.Duration)7 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)6 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)6 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)6 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)5 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)5