Create a Java Azure Cosmos DB Function Trigger using Visual Studio Code in 2 minutes! - Azure Cosmos DB Blog (original) (raw)

March 13th, 2020

0 reactions

Theo van Kraay

Principal Program Manager

Creating event sourcing solutions with Azure Cosmos DB is easy with Azure Functions Triggers, where you can leverage the Change Feed Processor‘s powerful scaling and reliable event detection functionality, without the need to maintain any worker infrastructure. You can just focus on your Azure Function’s logic without worrying about the rest of the event-sourcing pipeline. In this blog, we have some quick how-to videos to get you up and running with Java Cosmos DB Functions Triggers!

If you want to following along, there are some pre-requisites you should have in place:

Create an Azure Cosmos DB Functions Trigger in Java with VS Code… in 2 minutes!

(code shown below)

The code used in the video is below:

package com.function;

import com.microsoft.azure.functions.annotation.; import com.microsoft.azure.functions.;

/**

}

The local.settings.json file should look something like the below:

{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "AzureCosmosDBConnection": "", "FUNCTIONS_WORKER_RUNTIME": "java" } }

Next, you can turn your function into a simple event sourcing solution, which streams updates and inserts from one collection into another, by replacing the code created in the first demo, with the below:

package com.function;

import com.microsoft.azure.functions.annotation.; import com.microsoft.azure.cosmosdb.ConnectionMode; import com.microsoft.azure.cosmosdb.ConnectionPolicy; import com.microsoft.azure.cosmosdb.ConsistencyLevel; import com.microsoft.azure.cosmosdb.Document; import com.microsoft.azure.cosmosdb.rx.AsyncDocumentClient; import com.microsoft.azure.functions.;

/**

}

Your local.settings.json would look something like this (note the targeturi and targeturikey added for the target collection):

{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "AzureCosmosDBConnection": "", "FUNCTIONS_WORKER_RUNTIME": "java", "targeturi": "<URI for target database/collection from Portal>", "targeturikey": "<PRIMARY KEY for target database/collection from Portal>" } }

Watch the video below to see how to deploy to Azure! To follow along with this video, you should have the pre-requisites from the above video already installed, plus the following:

com.microsoft.azure azure-cosmosdb 2.6.5

For more information about Azure Cosmos DB’s change feed and it’s use cases, go here!

For the official documentation on creating an Azure Function triggered by Cosmos DB, go here!

Get started

Create a new account using the Azure Portal, ARM template or Azure CLI and connect to it using your favourite tools. Stay up-to-date on the latest Azure #CosmosDB news and features by following us on Twitter @AzureCosmosDB. We are really excited to see what you will build with Azure Cosmos DB!

About Azure Cosmos DB

Azure Cosmos DB is a globally distributed, multi-model NoSQL database service that enables you to read and write data from any Azure region. It offers turnkey global distribution, guarantees single-digit millisecond latency at the 99th percentile, 99.999 percent high availability, with elastic scaling of throughput and storage.

Author

Theo van Kraay

Principal Program Manager

Principal Program Manager on the Azure Cosmos DB engineering team. Currently focused on AI, programmability, and developer experience for Azure Cosmos DB.