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

标签 python azure azure-functions azure-eventhub

我在 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)

第一个(事件)充当事件中心触发器,另外 2 个是目标事件中心。我不能使用 $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 个事件中心,但这感觉更像是一个 hacky 解决方案。如果我错过了什么,请告诉我。

最佳答案

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

关于python - Azure 函数与 Python |如何输出到多个事件中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61725194/

相关文章:

c# - 在 AzureFunction 上使用 RSACryptoServiceProvider

c# - Azure Functions DocumentClient 无法从程序集异常加载类型

c# - 如果 secret 中的值发生变化, key 保管库值不会更新,因为它会为 secret 生成新版本

python sqlalchemy 在元组数据结构中插入多行

python - Pandas 旋转一列,同时使用相同的列值作为列标题

python - 使用 get_absolute_url() 时出现 NoReverseMatch 错误

Azure 云服务和 Amazon Cloudfront 互操作性

python - 从类 (datetime.date) 继承会导致 super().__init__(...) 参数过多 TypeError

java - 尝试使用 java 实现 Azure AD 身份验证时出现异常

azure - 如何登录 Azure 服务主体