带有Python的Azure函数|如何输出到多个事件中心

问题描述 投票:0回答:1

我已经在Python中创建了一个Azure函数,该函数从事件中心触发,并在其计算后输出到两个给定的事件中心之一。我尝试了以下操作,但不起作用:

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

第一个(事件)充当事件中心触发器,其他两个充当目标事件中心。我不能使用$ return,因为我打算有多个输出。错误提示:

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 '

function.json中的相应部分:

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

作为最后的手段,我使用了普通的Python EventHub生产者客户端,并将其连接到2个事件中心,但这感觉更像是个骇人听闻的解决方案。请让我知道我是否想念任何东西。

python azure azure-functions azure-eventhub
1个回答
0
投票

好吧,我找到了一个可行的解决方案。我不确定这是否是理想的选择,但确实可行。问题是“ func.Out [func.EventHubEvent]”和“ type”:“ eventHub”之间的类型不匹配。所以我将其更改为func.Out [str],现在我将对象转换为字符串

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))
© www.soinside.com 2019 - 2024. All rights reserved.