Search in sources :

Example 6 with CosmosItemResponse

use of com.azure.cosmos.models.CosmosItemResponse in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class CosmosDiagnosticsQuickStartAsync method deleteDocument.

// Document delete
private void deleteDocument() throws Exception {
    logger.info("Delete document by ID {}", documentId);
    // Delete document
    Mono<CosmosItemResponse<Object>> itemResponseMono = container.deleteItem(documentId, new PartitionKey(documentLastName), new CosmosItemRequestOptions());
    CosmosItemResponse<Object> itemResponse = itemResponseMono.block();
    CosmosDiagnostics diagnostics = itemResponse.getDiagnostics();
    logger.info("Delete item diagnostics : {}", diagnostics);
    logger.info("Done.");
}
Also used : CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosDiagnostics(com.azure.cosmos.CosmosDiagnostics)

Example 7 with CosmosItemResponse

use of com.azure.cosmos.models.CosmosItemResponse in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class CosmosDiagnosticsQuickStartAsync 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.
    Mono<CosmosItemResponse<Family>> itemResponseMono = container.createItem(family, new PartitionKey(family.getLastName()), new CosmosItemRequestOptions());
    CosmosItemResponse<Family> itemResponse = itemResponseMono.block();
    CosmosDiagnostics diagnostics = itemResponse.getDiagnostics();
    logger.info("Create item diagnostics : {}", diagnostics);
    logger.info("Done.");
}
Also used : CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) Family(com.azure.cosmos.examples.common.Family) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosDiagnostics(com.azure.cosmos.CosmosDiagnostics)

Example 8 with CosmosItemResponse

use of com.azure.cosmos.models.CosmosItemResponse in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class CosmosDiagnosticsQuickStartAsync method readDocumentById.

// Document read
private void readDocumentById() throws Exception {
    logger.info("Read document by ID : {}", documentId);
    // Read document by ID
    Mono<CosmosItemResponse<Family>> itemResponseMono = container.readItem(documentId, new PartitionKey(documentLastName), Family.class);
    CosmosItemResponse<Family> familyCosmosItemResponse = itemResponseMono.block();
    CosmosDiagnostics diagnostics = familyCosmosItemResponse.getDiagnostics();
    logger.info("Read item diagnostics : {}", diagnostics);
    Family family = familyCosmosItemResponse.getItem();
    // Check result
    logger.info("Finished reading family {} with partition key {}", family.getId(), family.getLastName());
    logger.info("Done.");
}
Also used : CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) PartitionKey(com.azure.cosmos.models.PartitionKey) Family(com.azure.cosmos.examples.common.Family) CosmosDiagnostics(com.azure.cosmos.CosmosDiagnostics)

Example 9 with CosmosItemResponse

use of com.azure.cosmos.models.CosmosItemResponse in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SampleRequestThroughput method requestThroughputDemo.

public static void requestThroughputDemo() {
    client = new CosmosClientBuilder().endpoint(AccountSettings.HOST).key(AccountSettings.MASTER_KEY).consistencyLevel(ConsistencyLevel.EVENTUAL).contentResponseOnWriteEnabled(true).buildClient();
    // This code synchronously sends a request to create a database.
    // While the client waits for a response, this thread is blocked from
    // performing other tasks.
    CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists("ContosoInventoryDB");
    database = client.getDatabase(databaseResponse.getProperties().getId());
    logger.info("\n\n\n\nCreated database ContosoInventoryDB.\n\n\n\n");
    // IndexingPolicy indexingPolicy = new IndexingPolicy();
    // indexingPolicy.setIndexingMode(IndexingMode.NONE);
    // indexingPolicy.setAutomatic(false);
    CosmosContainerProperties containerProperties = new CosmosContainerProperties("ContosoInventoryContainer", "/id");
    // containerProperties.setIndexingPolicy(indexingPolicy);
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties, throughputProperties);
    container = database.getContainer(containerResponse.getProperties().getId());
    logger.info("\n\n\n\nCreated container ContosoInventoryContainer.\n\n\n\n");
    // Resources are ready.
    // 
    // Create many docs to insert into the container
    int number_of_docs = 50000;
    logger.info("Generating {} documents...", number_of_docs);
    ArrayList<JsonNode> docs = Profile.generateDocs(number_of_docs);
    logger.info("Inserting {} documents...", number_of_docs);
    Profile.tic();
    // Profiler code - it's good for this part to be async
    Flux.interval(Duration.ofMillis(10)).map(tick -> {
        // logger.info("In profiler.");
        toc_time = Profile.toc_ms();
        current_docs_inserted = number_docs_inserted.get();
        current_total_charge = total_charge.get();
        if (toc_time >= 1000.0) {
            Profile.tic();
            rps = 1000.0 * ((double) (current_docs_inserted - last_docs_inserted)) / toc_time;
            rups = 1000.0 * (current_total_charge - last_total_charge) / toc_time;
            logger.info(String.format("\n\n\n\n" + "Sync Throughput Profiler Result, Last 1000ms:" + "\n\n" + "%8s          %8s", StringUtils.center("Req/sec", 8), StringUtils.center("RU/s", 8)) + "\n" + "----------------------------------" + "\n" + String.format("%8.1f          %8.1f", rps, rups) + "\n\n\n\n");
            last_docs_inserted = current_docs_inserted;
            last_total_charge = current_total_charge;
        }
        return tick;
    }).subscribe();
    // While the client is waiting for a response, the thread is blocked from other tasks
    for (JsonNode doc : docs) {
        CosmosItemResponse<JsonNode> itemResponse = container.createItem(doc);
        if (itemResponse.getStatusCode() == 201) {
            number_docs_inserted.getAndIncrement();
            total_charge.getAndAdd((int) itemResponse.getRequestCharge());
        } else
            logger.warn("WARNING insert status code {} != 201", itemResponse.getStatusCode());
    }
    // Clean up
    logger.info("Deleting resources.");
    container.delete();
    database.delete();
    logger.info("Finished deleting resources.");
    logger.info("Closing client...");
    client.close();
    logger.info("Done with demo.");
}
Also used : Profile(com.azure.cosmos.examples.common.Profile) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) Logger(org.slf4j.Logger) CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) LoggerFactory(org.slf4j.LoggerFactory) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Flux(reactor.core.publisher.Flux) CosmosClient(com.azure.cosmos.CosmosClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CosmosContainer(com.azure.cosmos.CosmosContainer) CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) Duration(java.time.Duration) ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) JsonNode(com.fasterxml.jackson.databind.JsonNode) AccountSettings(com.azure.cosmos.examples.common.AccountSettings) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) JsonNode(com.fasterxml.jackson.databind.JsonNode) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Aggregations

CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)9 CosmosDiagnostics (com.azure.cosmos.CosmosDiagnostics)5 Family (com.azure.cosmos.examples.common.Family)5 PartitionKey (com.azure.cosmos.models.PartitionKey)5 CosmosItemRequestOptions (com.azure.cosmos.models.CosmosItemRequestOptions)4 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)3 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)2 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)2 AccountSettings (com.azure.cosmos.examples.common.AccountSettings)2 CustomPOJO (com.azure.cosmos.examples.common.CustomPOJO)2 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)2 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)2 CosmosDatabaseResponse (com.azure.cosmos.models.CosmosDatabaseResponse)2 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Flux (reactor.core.publisher.Flux)2 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)1