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

2024-05-19 22:47:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在Python中创建了azure函数,该函数由事件中心触发,并在计算后输出到2个给定事件中心之一。我尝试了以下方法,但无效:

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 producer客户端并将其连接到两个事件中心,但这感觉更像是一个黑客解决方案。如果我错过了什么,请告诉我


Tags: 函数injsontype事件functionoutfunctions
1条回答
网友
1楼 · 发布于 2024-05-19 22:47:41

好的,我找到了一个有效的解决方案。我不确定这是否是理想的,但它确实有效。问题是“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))

相关问题 更多 >