4

I'm trying to connect an Azure DocumentDB and save documents using Azure Functions but I don't know how to create the connection.

3
  • What have you tried so far, within your Azure Function? Please edit your question with more information. As your question currently stands, it's not really clear where you're stuck. Commented Aug 2, 2016 at 14:17
  • For now I created the database and created an azure function but I don't know how I can connect with the database to list, update, create, etc Commented Aug 2, 2016 at 21:44
  • It's the same way as though you were doing on ASPNET app, Console App and so on. You're able to find an example here on my repo Commented Jul 3, 2017 at 13:36

5 Answers 5

4

You can do it using the Azure Portal. After you created the DocumentDB -

  • Create new Azure Function.
  • Go to the Integrate Tab.
  • You can choose Azure Document DB as an output for your function.
  • Choose your Document DB/Database Name/Collection you want to use.
  • Document parameter name is the Output of your function.

For example

using System;

public static void Run(string input, out object document, TraceWriter log)
{
    log.Info($"C# manually triggered function called with input: {input}");
    document = new {
        text = $"I'm running in a C# function! {input}"
    };
}

you need to provide out object which is the same as you defined in the output tab.

Sign up to request clarification or add additional context in comments.

6 Comments

And how I connect with the database and get the information?
@LuísFura If you want to query document Db you have many guides online. For Example - azure.microsoft.com/en-us/documentation/articles/…
I tried to add the azure.documents reference and I got an error message. Where I can add the references to my function?
here you have a guide how to add reference to azure functions - azure.microsoft.com/en-us/documentation/articles/… also, from SO - stackoverflow.com/questions/36411536/…
I just recently answered a similar question for access to DocumentClient from within a script: social.msdn.microsoft.com/Forums/azure/en-US/…
|
1

You can just use the document client directly:

var endpoint = "https://XXXXX.documents.azure.com:443/";
var authKey = "XXXXX";

using (var client = new DocumentClient(new Uri(endpoint), authKey))
{
    var sqlCountQuery = "select value count(1) from c";
    IDocumentQuery<dynamic> query = client.CreateDocumentQuery<dynamic>(UriFactory.CreateDocumentCollectionUri("YOUR_DB_ID", "YOUR_COLLECTON_ID"), sqlCountQuery).AsDocumentQuery();
    ....
}

Comments

1

Azure Functions supports Document DB (Cosmos DB) out-of-the-box. You can just simply add an environment variable called AzureWebJobsDocumentDBConnectionString in V1 or AzureWebJobsCosmosDBConnectionString in V2.

Then just use a CosmosDBTrigger binding attribute for input binding like (in C# for example):

public static class UpsertProductCosmosDbTrigger
{
    [FunctionName("ProductUpsertCosmosDbTrigger")]
    public static void Run(
        [CosmosDBTrigger(
        // Those names come from the application settings.
        // Those names can come with both preceding % and trailing %.
        databaseName: "CosmosDbDdatabaseName",
        collectionName: "CosmosDbCollectionName",
        LeaseDatabaseName = "CosmosDbDdatabaseName",
        LeaseCollectionName = "CosmosDbLeaseCollectionName")] 
        IReadOnlyList<Document> input,

        TraceWriter log)
...

For output binding use DocumentDB output binding attribute in V1 and CosmosDB in V2 like:

[FunctionName("ProductUpsertHttpTrigger")]
public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = "products")] 
    HttpRequestMessage req,

    [DocumentDB(
    databaseName: "%CosmosDbDdatabaseName%",
    collectionName: "%CosmosDbCollectionName%")] IAsyncCollector<Product> collector,

    TraceWriter log)
...

I've written a blog post about this: https://blog.mexia.com.au/cosmos-db-in-azure-functions-v1-and-v2

Comments

0
var EndpointUrl = "EndpointUrl";
var PrimaryKey = "PrimaryKeyValue" 
this.client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
Database database = await this.client.CreateDatabaseIfNotExistsAsync(new Database { Id = cosmoDbName });

you can get the End-point-URL and Primary-Key value from the azure portal in the keys section.

Comments

0

Assume C# has similar SDK like Java. The below is for Java

There are two ways you can connect to documentDB from an Azure function.

  1. Using SDK

    DocumentClient documentClient = new DocumentClient( "SERVICE_ENDPOINT", "MASTER_KEY", ConnectionPolicy.GetDefault(), ConsistencyLevel.Session);

Refer - [https://learn.microsoft.com/en-us/azure/cosmos-db/sql-api-java-samples][1]. This has .Net Samples too.

  1. Binding

    @FunctionName("CosmosDBStore") @CosmosDBOutput(name = "database", databaseName = "db_name", collectionName = "col_name", connectionStringSetting = "AzureCosmosDBConnection")

Please make sure you have a variable in the name of "AzureCosmosDBConnection" in your application settings and local.settings.json(if you want to test locally)

Refer - [https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2][1]

The above link has C# example too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.