Search in sources :

Example 56 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 patchIncrement.

private void patchIncrement(String id, String partitionKey) {
    logger.info("Executing Patch with 'increment' operations");
    CosmosPatchOperations cosmosPatchOperations = CosmosPatchOperations.create();
    cosmosPatchOperations.add("/int", 42).increment("/int", 1).increment("/int", -1).add("/float", 42).increment("/float", 4.2).increment("/float", -4.2);
    CosmosPatchItemRequestOptions options = new CosmosPatchItemRequestOptions();
    try {
        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) CosmosPatchOperations(com.azure.cosmos.models.CosmosPatchOperations)

Example 57 with Family

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

the class QueriesQuickstart method executeQueryWithQuerySpecPrintSingleResult.

private void executeQueryWithQuerySpecPrintSingleResult(SqlQuerySpec querySpec) {
    logger.info("Execute query {}", querySpec.getQueryText());
    CosmosPagedIterable<Family> filteredFamilies = container.queryItems(querySpec, new CosmosQueryRequestOptions(), Family.class);
    // Print
    if (filteredFamilies.iterator().hasNext()) {
        Family family = filteredFamilies.iterator().next();
        logger.info(String.format("First query result: Family with (/id, partition key) = (%s,%s)", family.getId(), family.getLastName()));
    }
    logger.info("Done.");
}
Also used : CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) Family(com.azure.cosmos.examples.common.Family)

Example 58 with Family

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

the class QueriesQuickstart method createDocument.

private void createDocument() throws Exception {
    logger.info("Create document " + documentId);
    // Define a document as a POJO (internally this
    // is converted to JSON via custom serialization)
    Family family = new Family();
    family.setLastName(documentLastName);
    family.setId(documentId);
    // Insert this item as a document
    // Explicitly specifying the /pk value improves performance.
    container.createItem(family, new PartitionKey(family.getLastName()), new CosmosItemRequestOptions());
    logger.info("Done.");
}
Also used : CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey)

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