Austin
Austin

Reputation: 424

Azure Functions with Python | How to output to multiple event hubs

I have created azure functions in Python which is triggered from event hub and after its calculations outputs to one of the 2 given event hubs. I tried the following, but it doesn't work:

def main(event: func.EventHubEvent, eh1: func.Out[func.EventHubEvent], eh2: func.Out[func.EventHubEvent]):
    if condition: eh1.set(ev1)
    else: eh2.set(ev2)

The first (event) acts as the event hub trigger and the other 2 are destination event hubs. I can't use $return since i intend to have multiple outputs. Error given:

11/05/2020 08:10:54] Worker failed to function id 84b68627-224b-4626-93cb-xxxxxxx.
[11/05/2020 08:10:54] Result: Failure
[11/05/2020 08:10:54] Exception: FunctionLoadError: cannot load the EventHubTriggerFunction function: type of eh1 binding in function.json "eventHub" does not match its Python annotation "EventHubEvent"
[11/05/2020 08:10:54] Stack:   File "/usr/local/Cellar/azure-functions-core-tools/2.7.2508/workers/python/3.7/OSX/X64/azure_functions_worker/dispatcher.py", line 246, in _handle__function_load_request
[11/05/2020 08:10:54]     function_id, func, func_request.metadata)
[11/05/2020 08:10:54]   File "/usr/local/Cellar/azure-functions-core-tools/2.7.2508/workers/python/3.7/OSX/X64/azure_functions_worker/functions.py", line 216, in add_function
[11/05/2020 08:10:54]     f'type of {param.name} binding in function.json '

the corresponding section in function.json:

{
      "type": "eventHub",
      "name": "eh1",
      "eventHubName": "EventHubName",
      "connection": "ConnectionSetting",
      "direction": "out"
}

As a last resort i used plain Python EventHub producer client and connected it to the 2 event hubs but that feels more of a hacky solution. Please let me know if there was anything that i missed.

Upvotes: 2

Views: 1655

Answers (1)

Austin
Austin

Reputation: 424

Ok, I found a working solution for this. I am not sure if this is the ideal one but it does work. The problem was the type mismatch between "func.Out[func.EventHubEvent]" and "type": "eventHub". So I changed it func.Out[str] and now I am casting my objects to a string

def main(event: func.EventHubEvent, eh1: func.Out[str], eh2: func.Out[str]):

    ev1 = {"field1":"Sample field", "field2":"Sample field2"}  
    ev2 = {"field1":"field1Value", "field2":"field2Value", "paths":[
        "path/to/file/1",
        "path/to/file/2",
        "path/to/file/3"
    ]}

    logging.info("Sending events")
    eh1.set(json.dumps(ev1))
    eh2.set(json.dumps(ev2))

Upvotes: 2

Related Questions