title | description | ms.topic | ms.date | ms.devlang | ms.custom |
---|---|---|---|---|---|
Store unstructured data using Azure Cosmos DB and Functions |
Store unstructured data using Azure Functions and Cosmos DB |
quickstart |
10/01/2020 |
csharp, javascript |
devx-track-csharp, mvc, mode-other |
Azure Cosmos DB is a great way to store unstructured and JSON data. Combined with Azure Functions, Cosmos DB makes storing data quick and easy with much less code than required for storing data in a relational database.
Note
At this time, the Azure Cosmos DB trigger, input bindings, and output bindings work with SQL API and Graph API accounts only.
In Azure Functions, input and output bindings provide a declarative way to connect to external service data from your function. In this article, learn how to update an existing function to add an output binding that stores unstructured data in an Azure Cosmos DB document.
To complete this tutorial:
[!INCLUDE Previous quickstart note]
You must have an Azure Cosmos DB account that uses the SQL API before you create the output binding.
[!INCLUDE cosmos-db-create-dbaccount]
-
In the Azure portal, navigate to and select the function app you created previously.
-
Select Functions, and then select the HttpTrigger function.
:::image type="content" source="./media/functions-integrate-store-unstructured-data-cosmosdb/functions-select-http-function.png" alt-text="Select your Http function in the Azure portal." border="true":::
-
Select Integration and + Add output.
:::image type="content" source="./media/functions-integrate-store-unstructured-data-cosmosdb/functions-add-output-binding.png" alt-text="Add an Azure Cosmos DB output binding." border="true":::
-
Use the Create Output settings as specified in the table:
:::image type="content" source="./media/functions-integrate-store-unstructured-data-cosmosdb/functions-configure-cosmosdb-binding.png" alt-text="Configure Azure Cosmos DB output binding." border="true":::
Setting Suggested value Description Binding Type Azure Cosmos DB Name of the binding type to select to create the output binding to Azure Cosmos DB. Document parameter name taskDocument Name that refers to the Cosmos DB object in code. Database name taskDatabase Name of database to save documents. Collection name taskCollection Name of the database collection. If true, creates the Cosmos DB database and collection Yes The collection doesn't already exist, so create it. Cosmos DB account connection New setting Select New, then choose Azure Cosmos DB Account and the Database account you created earlier, and then select OK. Creates an application setting for your account connection. This setting is used by the binding to connection to the database. -
Select OK to create the binding.
Replace the existing function code with the following code, in your chosen language:
Replace the existing C# function with the following code:
#r "Newtonsoft.Json"
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
public static IActionResult Run(HttpRequest req, out object taskDocument, ILogger log)
{
string name = req.Query["name"];
string task = req.Query["task"];
string duedate = req.Query["duedate"];
// We need both name and task parameters.
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(task))
{
taskDocument = new
{
name,
duedate,
task
};
return (ActionResult)new OkResult();
}
else
{
taskDocument = null;
return (ActionResult)new BadRequestResult();
}
}
Replace the existing JavaScript function with the following code:
module.exports = async function (context, req) {
// We need both name and task parameters.
if (req.query.name && req.query.task) {
// Set the output binding data from the query object.
context.bindings.taskDocument = req.query;
// Success.
context.res = {
status: 200
};
}
else {
context.res = {
status: 400,
body: "The query options 'name' and 'task' are required."
};
}
};
This code sample reads the HTTP Request query strings and assigns them to fields in the taskDocument
object. The taskDocument
binding sends the object data from this binding parameter to be stored in the bound document database. The database is created the first time the function runs.
-
Select Test/Run. Under Query, select + Add parameter and add the following parameters to the query string:
name
task
duedate
:::image type="content" source="./media/functions-integrate-store-unstructured-data-cosmosdb/functions-test-function.png" alt-text="Test the function." border="true":::
-
Select Run and verify that a 200 status is returned.
:::image type="content" source="./media/functions-integrate-store-unstructured-data-cosmosdb/functions-test-function-output.png" alt-text="Screenshot shows the HTTP response code 200 status highlighted after selecting Run." border="true":::
-
In the Azure portal, search for and select Azure Cosmos DB.
:::image type="content" source="./media/functions-integrate-store-unstructured-data-cosmosdb/functions-search-cosmos-db.png" alt-text="Search for the Cosmos DB service." border="true":::
-
Choose your Azure Cosmos DB account, then select Data Explorer.
-
Expand the TaskCollection nodes, select the new document, and confirm that the document contains your query string values, along with some additional metadata.
:::image type="content" source="./media/functions-integrate-store-unstructured-data-cosmosdb/functions-data-explorer-check-document.png" alt-text="Verify the string values in your document." border="true":::
You've successfully added a binding to your HTTP trigger to store unstructured data in an Azure Cosmos DB.
[!INCLUDE Clean-up section]
For more information about binding to a Cosmos DB database, see Azure Functions Cosmos DB bindings.
[!INCLUDE functions-quickstart-next-steps]