Search in sources :

Example 16 with CosmosAsyncContainer

use of com.azure.cosmos.CosmosAsyncContainer 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)

Aggregations

CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)16 CustomPOJO (com.azure.cosmos.examples.common.CustomPOJO)9 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)7 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)7 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 ChangeFeedProcessor (com.azure.cosmos.ChangeFeedProcessor)4 ChangeFeedProcessorBuilder (com.azure.cosmos.ChangeFeedProcessorBuilder)4 CosmosItemRequestOptions (com.azure.cosmos.models.CosmosItemRequestOptions)4 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)4 PartitionKey (com.azure.cosmos.models.PartitionKey)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 Scheduler (reactor.core.scheduler.Scheduler)4 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)3 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)3 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)3 Family (com.azure.cosmos.examples.common.Family)3 ExcludedPath (com.azure.cosmos.models.ExcludedPath)3 IncludedPath (com.azure.cosmos.models.IncludedPath)3 IndexingPolicy (com.azure.cosmos.models.IndexingPolicy)3