none
Azure Functions with connection to Cosmos DB RRS feed

  • Question

  • Hello,

    I'm still learning Azure with a lot of progress but I would like to ask for an advice more advanced users. Is it possible to create Azure Function (with Event Hub trigger) with connection to Cosmos DB? I'll try to explain it.


        1. To Event Hub (Azure Function trigger) comes some input / message (json file)
        2. In Azure Function there are some transformations of this message - (preferred JavaScript language)
        3. There is Connection to Cosmos DB and some other modification (preferred JavaScript language)
        4. After that process, message comes into Event Hub output.


    My question is if it is possible to connect with Cosmos DB? Exemplary scenario:

    Incoming message to Azure Function in json format has a position dog_name: "Jimmy". And I would like to check if in my database - collection in Cosmos DB (also json format) with position "Dog_names", there is "Jimmy" and add to Event Hub output - yes or no message.

    This is just an example to show the general idea. I hope someone would be able to understand something from this :)

    Thanks in advance for every advice.

    Sunday, February 25, 2018 9:40 AM

All replies

  • @tasse09

    Everything you are asking is completely possible and can all be done through input and output bindings. There is no quick answer to your question though as it requires fingers in the keyboard and knowledge. To expand your knowledge and really make use of Azure Functions a complete understanding is extremely important. There are several Pluralsight courses on Azure Functions but the one I will recommend as the instructor goes through bindings and everything else fairly well is:

    https://app.pluralsight.com/library/courses/azure-functions-fundamentals

    And if you don't want a course but just need the documentation then it is here:

    https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings

    Hope that helps and if it does be sure to mark as answered!

    Azure on my friend!

    ~Theo

    Sunday, February 25, 2018 9:47 PM
  • Hi Theo,

    thank you very much for those links! It is now much more clear to me and with documentation I have done almost everything with my first learning. 

    I have only one more question. I've done a binding in Azure Function with CosmosDB, it looks for example like this:

    {
          "type": "documentDB",
          "name": "inDoc",
          "databaseName": "test",
          "sqlQuery": "SELECT * FROM a where a.name = {identify}",
          "collectionName": "collection",
          "connection": "connect",
          "direction": "in"
        }


    and my code and whole function works perfect with cosmos DB and Event hub trigger.

    But what if I would like to use more expanded json input. It works if I have:

    {
    "identify": "somename"
    "color" :"some"
    }

    but with more complex json input, like:

       
    {
    "anmial": {
        "identify": "somename"
        },
    "color" :"some"
    }

    the query SELECT * FROM a where a.name = {identify}" obviously is not working with this json but also it is not working: 
    "...where a.name = {annimal.identify}" 
    or using GetArrayElement.

    Is there a simply solution?

    And other quick question - yes/no is will be enough - I can do further research :) :
    I can use sqlQuery in CosmosDB bindings, but is it also possible to use it in javascript code to operate on cosmosDB input?
      

    Wednesday, February 28, 2018 3:05 PM
  • @Tasse09

    First question: Yes you can get more complex, take a look at the doc on Cosmos bindings specifically:

    https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb

    Second question, answer is yes and the same document above has some samples.

    Hope that helps and if it does be sure to mark as answered!

    Azure on my friend!

    ~Theo


    Wednesday, February 28, 2018 3:24 PM
  • when I tried a nested JSON structure it is giving error as follows

     Microsoft.Azure.WebJobs.Host: Error while accessing 'identify': property doesn't exist.
    

    Saturday, May 5, 2018 3:58 AM
  • That should be  {annimal}.identify
    Saturday, May 5, 2018 5:20 AM