Azure Function EventHub Trigger and CosmosDB Output in Python

Lukas Reber 20 Reputation points
2024-02-26T10:45:30.5133333+00:00

Hi, I'm trying to write a Azure Function in Python which accepts messages from an EventHub and puts them into a CosmosDB. I've tried with the following basic example code but it doesnt work. As soon as I add the binding for the cosmos db, the function is not visible inside the azure portal anymore, so something must be wrong here i guess :-) Any help is appreciated!

import azure.functions as func 
import logging  app = func.FunctionApp()
  
@app.event_hub_message_trigger(arg_name="azeventhub", event_hub_name="testeventhub",                                connection="CONNECTIONSTRING")  
@app.cosmos_db_output(arg_name="documents",database_name="testdatabase", collection_name="testcollection",create_if_not_exists=True,connection_string_setting="TestAccess_COSMOSDB") 
def eventhub_trigger(azeventhub: func.EventHubEvent, documents: func.Out[func.Document]):     	
	logging.info('Python EventHub trigger processed an event: %s',
	azeventhub.get_body().decode('utf-8'))
	documents.set(event.get_body().decode('utf-8'))

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,565 questions
Azure Event Hubs
Azure Event Hubs
An Azure real-time data ingestion service.
591 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,517 questions
0 comments No comments
{count} votes

Accepted answer
  1. MayankBargali-MSFT 69,946 Reputation points
    2024-02-27T11:31:36.13+00:00

    @Lukas Reber I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others", I'll repost your solution in case you'd like to "Accept" the answer.

    Issue:

    You observed that when trying to write a Azure Function in Python which accepts messages from an EventHub and puts them into a CosmosDB. You have tried with the following basic example code but it doesn't work. As soon as I add the binding for the cosmos db, the function is not visible inside the azure portal anymore

     import azure.functions as func 
    import logging  app = func.FunctionApp()
      
    @app.event_hub_message_trigger(arg_name="azeventhub", event_hub_name="testeventhub",                                connection="CONNECTIONSTRING")  
    @app.cosmos_db_output(arg_name="documents",database_name="testdatabase", collection_name="testcollection",create_if_not_exists=True,connection_string_setting="TestAccess_COSMOSDB") 
    def eventhub_trigger(azeventhub: func.EventHubEvent, documents: func.Out[func.Document]):     	
    	logging.info('Python EventHub trigger processed an event: %s',
    	azeventhub.get_body().decode('utf-8'))
    	documents.set(event.get_body().decode('utf-8'))
    

    Solution:

    You have changed the parameter for cosmos_db_output are container_name (instead of collection_name) and connection (instead of connection_string_settings).

    Yes, your understanding is correct. In newer version (extension 4+), the property was changed to container_name from collection_name and connection from connection_string_setting respectively.

    The same is documented here and sample here.

    Unless otherwise noted, examples in this article target version 3.x of the Azure Cosmos DB extension. For use with extension version 4.x, you need to replace the string collection in property and attribute names with container.

    I don't see specially calling out for connection parameter and I will reach out to the content team to review it further.

    Thank you again for your time and patience throughout this issue.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Amira Bedhiafi 18,501 Reputation points
    2024-02-26T11:19:33.6233333+00:00

    It looks like the issue might be related to the configuration of the CosmosDB output binding in your Azure Function. The Azure Function not being visible in the Azure portal after adding the CosmosDB binding suggests there could be a misconfiguration or a syntax issue with how the binding is set up in your code. Another thing, in your function code, you're attempting to set the document content with documents.set(event.get_body().decode('utf-8')). However, your Event Hub trigger parameter is named azeventhub, not event. This should be corrected to documents.set(azeventhub.get_body().decode('utf-8')).

    1 person found this answer helpful.

  2. Lukas Reber 20 Reputation points
    2024-02-27T10:10:44.7966667+00:00

    Ok turns out that the correct parameter for cosmos_db_output are container_name (instead of collection_name) and connection (instead of connection_string_settings). I'm not sure if this is because of some version mismatch, but it works now.

    0 comments No comments