Search in sources :

Example 6 with CustomPOJO

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

the class SalesOrder method TroubleshootingGuideJavaSDKv4NeedsSchedulerAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/troubleshoot-java-sdk-v4-sql
 * Troubleshooting guide - needs scheduler
 * Async only
 */
/**
 * Troubleshooting guide - needs scheduler
 */
public static void TroubleshootingGuideJavaSDKv4NeedsSchedulerAsync() {
    CosmosAsyncContainer container = null;
    CustomPOJO item = null;
    // <TroubleshootNeedsSchedulerAsync>
    // Bad code with read timeout exception
    int requestTimeoutInSeconds = 10;
    /* ... */
    AtomicInteger failureCount = new AtomicInteger();
    // Max number of concurrent item inserts is # CPU cores + 1
    Flux<Family> familyPub = Flux.just(Families.getAndersenFamilyItem(), Families.getAndersenFamilyItem(), Families.getJohnsonFamilyItem());
    familyPub.flatMap(family -> {
        return container.createItem(family);
    }).flatMap(r -> {
        try {
            // Time-consuming work is, for example,
            // writing to a file, computationally heavy work, or just sleep.
            // Basically, it's anything that takes more than a few milliseconds.
            // Doing such operations on the IO Netty thread
            // without a proper scheduler will cause problems.
            // The subscriber will get a ReadTimeoutException failure.
            TimeUnit.SECONDS.sleep(2 * requestTimeoutInSeconds);
        } catch (Exception e) {
        }
        return Mono.empty();
    }).doOnError(Exception.class, exception -> {
        failureCount.incrementAndGet();
    }).blockLast();
    assert (failureCount.get() > 0);
// </TroubleshootNeedsSchedulerAsync>
}
Also used : PartitionKey(com.azure.cosmos.models.PartitionKey) LoggerFactory(org.slf4j.LoggerFactory) ExcludedPath(com.azure.cosmos.models.ExcludedPath) Scheduler(reactor.core.scheduler.Scheduler) ArrayList(java.util.ArrayList) ProxyOptions(com.azure.core.http.ProxyOptions) DirectConnectionConfig(com.azure.cosmos.DirectConnectionConfig) Families(com.azure.cosmos.examples.common.Families) Family(com.azure.cosmos.examples.common.Family) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) Duration(java.time.Duration) Schedulers(reactor.core.scheduler.Schedulers) ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) JsonNode(com.fasterxml.jackson.databind.JsonNode) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) ExecutorService(java.util.concurrent.ExecutorService) ChangeFeedProcessorBuilder(com.azure.cosmos.ChangeFeedProcessorBuilder) CustomPOJO(com.azure.cosmos.examples.common.CustomPOJO) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy) Logger(org.slf4j.Logger) GatewayConnectionConfig(com.azure.cosmos.GatewayConnectionConfig) IncludedPath(com.azure.cosmos.models.IncludedPath) IndexingMode(com.azure.cosmos.models.IndexingMode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Mono(reactor.core.publisher.Mono) ChangeFeedProcessor(com.azure.cosmos.ChangeFeedProcessor) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) List(java.util.List) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) ConflictResolutionPolicy(com.azure.cosmos.models.ConflictResolutionPolicy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) Family(com.azure.cosmos.examples.common.Family) CustomPOJO(com.azure.cosmos.examples.common.CustomPOJO) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 7 with CustomPOJO

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

the class SalesOrder method PerformanceTipsJavaSDKv4NoPKSpecAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips-java-sdk-v4-sql
 * Performance tips - not specifying partition key in point-writes
 */
/**
 * Performance tips - not specifying partition key in point-writes
 */
public static void PerformanceTipsJavaSDKv4NoPKSpecAsync() {
    CosmosAsyncContainer asyncContainer = null;
    CustomPOJO item = null;
    String pk = "pk_value";
    // <PerformanceNoPKAsync>
    asyncContainer.createItem(item, new PartitionKey(pk), new CosmosItemRequestOptions()).block();
// </PerformanceNoPKAsync>
}
Also used : CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) PartitionKey(com.azure.cosmos.models.PartitionKey) CustomPOJO(com.azure.cosmos.examples.common.CustomPOJO)

Example 8 with CustomPOJO

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

the class SalesOrder method PerformanceTipsJavaSDKv4AddPKSpecAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips-java-sdk-v4-sql
 * Performance tips - add partition key in point-writes
 */
/**
 * Performance tips - add partition key in point-writes
 */
public static void PerformanceTipsJavaSDKv4AddPKSpecAsync() {
    CosmosAsyncContainer asyncContainer = null;
    CustomPOJO item = null;
    // <PerformanceAddPKAsync>
    asyncContainer.createItem(item).block();
// </PerformanceAddPKAsync>
}
Also used : CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CustomPOJO(com.azure.cosmos.examples.common.CustomPOJO)

Example 9 with CustomPOJO

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

the class SalesOrder method MigrateJavaSDKv4CFAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/migrate-java-v4-sdk
 * Change Feed
 */
/**
 * Change Feed
 */
public static void MigrateJavaSDKv4CFAsync() {
    String hostName = "hostname";
    String partition_key = "/pk";
    CosmosAsyncContainer feedContainer = null;
    CosmosAsyncContainer leaseContainer = null;
    // <MigrateCFAsync>
    ChangeFeedProcessor changeFeedProcessorInstance = new ChangeFeedProcessorBuilder().hostName(hostName).feedContainer(feedContainer).leaseContainer(leaseContainer).handleChanges((List<JsonNode> docs) -> {
        logger.info("--->setHandleChanges() START");
        for (JsonNode document : docs) {
            try {
                // Change Feed hands the document to you in the form of a JsonNode
                // As a developer you have two options for handling the JsonNode document provided to you by Change Feed
                // One option is to operate on the document in the form of a JsonNode, as shown below. This is great
                // especially if you do not have a single uniform data model for all documents.
                logger.info("---->DOCUMENT RECEIVED: " + OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(document));
                // You can also transform the JsonNode to a POJO having the same structure as the JsonNode,
                // as shown below. Then you can operate on the POJO.
                CustomPOJO pojo_doc = OBJECT_MAPPER.treeToValue(document, CustomPOJO.class);
                logger.info("----=>id: " + pojo_doc.getId());
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        logger.info("--->handleChanges() END");
    }).buildChangeFeedProcessor();
    // ...
    changeFeedProcessorInstance.start().subscribeOn(Schedulers.elastic()).subscribe();
// </MigrateCFAsync>
}
Also used : ChangeFeedProcessor(com.azure.cosmos.ChangeFeedProcessor) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) ChangeFeedProcessorBuilder(com.azure.cosmos.ChangeFeedProcessorBuilder) JsonNode(com.fasterxml.jackson.databind.JsonNode) CustomPOJO(com.azure.cosmos.examples.common.CustomPOJO) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 10 with CustomPOJO

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

the class SampleStoredProcedure method executeStoredProcedureArrayArg.

public void executeStoredProcedureArrayArg() throws Exception {
    logger.info(String.format("Executing stored procedure %s...\n\n", sprocId + "ArrayArg"));
    String partitionValue = "Seattle";
    CosmosStoredProcedureRequestOptions options = new CosmosStoredProcedureRequestOptions();
    options.setPartitionKey(new PartitionKey(partitionValue));
    List<Object> pojos = new ArrayList<>();
    pojos.add(new CustomPOJO("idA", partitionValue));
    pojos.add(new CustomPOJO("idB", partitionValue));
    pojos.add(new CustomPOJO("idC", partitionValue));
    List<Object> sproc_args = new ArrayList<>();
    sproc_args.add(OBJECT_MAPPER.writeValueAsString(pojos));
    CosmosStoredProcedureResponse executeResponse = container.getScripts().getStoredProcedure(sprocId + "ArrayArg").execute(sproc_args, options);
    logger.info(String.format("Stored procedure %s returned %s (HTTP %d), at cost %.3f RU.\n", sprocId + "ArrayArg", executeResponse.getResponseAsString(), executeResponse.getStatusCode(), executeResponse.getRequestCharge()));
}
Also used : CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) ArrayList(java.util.ArrayList) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosStoredProcedureResponse(com.azure.cosmos.models.CosmosStoredProcedureResponse) CustomPOJO(com.azure.cosmos.examples.common.CustomPOJO)

Aggregations

CustomPOJO (com.azure.cosmos.examples.common.CustomPOJO)12 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)8 PartitionKey (com.azure.cosmos.models.PartitionKey)4 CosmosContainer (com.azure.cosmos.CosmosContainer)3 CosmosItemRequestOptions (com.azure.cosmos.models.CosmosItemRequestOptions)3 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)3 ChangeFeedProcessor (com.azure.cosmos.ChangeFeedProcessor)2 ChangeFeedProcessorBuilder (com.azure.cosmos.ChangeFeedProcessorBuilder)2 CosmosStoredProcedureRequestOptions (com.azure.cosmos.models.CosmosStoredProcedureRequestOptions)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayList (java.util.ArrayList)2 ExecutorService (java.util.concurrent.ExecutorService)2 Scheduler (reactor.core.scheduler.Scheduler)2 ProxyOptions (com.azure.core.http.ProxyOptions)1 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)1 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)1 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)1 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)1 DirectConnectionConfig (com.azure.cosmos.DirectConnectionConfig)1