如何在Python函数中使用Azure函数命令输出绑定?

发布于 2025-02-10 07:58:56 字数 1890 浏览 2 评论 0原文

我正在尝试设置Azure函数,以从EventHub“ A”接收消息,然后根据使用功能绑定的输入消息将消息发送到EventHub“ B”或C的一个。

Azure函数支持EventHub的触发和输出绑定,但是,我不知道如何正确设置势在必行的输出绑定。

到目前为止,这是我的函数。 tabs =过程中%2cfunctionsv2%2cextensionv5& pivots =编程语言 - 语言 - python#usage'rel =“ nofollow noreferrer”> docs :

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "event",
      "type": "eventHubTrigger",
      "direction": "in",
      "eventHubName": "A",
      "consumerGroup": "myConsumerGroup",
      "connection": "aConnectionString"
    },
    {
      "type": "eventHub",
      "name": "B",
      "eventHubName": "B",
      "connection": "bConnectionString",
      "direction": "out"
    },
    {
      "type": "eventHub",
      "name": "C",
      "eventHubname": "C",
      "connection": "cConnectionString",
      "direction": "out"
    }
  ]
}
import logging
import azure.functions as func

def main(event: func.EventHubEvent, B: func.Out, C: func.Out):
  logging.info(f'########################################')
  logging.info(f'Function triggered to process a message:')
  logging.info(f'  EnqueuedTimeUtc = {event.enqueued_time}')
  logging.info(f'  SequenceNumber = {event.sequence_number}')
  logging.info(f'  Offset = {event.offset}')
  logging.info(f'----------------------------------------')
  logging.info(f'  Metadata: ')
  for key in event.metadata:
    logging.info(f'    key {key} = {event.metadata[key]}')
  logging.info(f'  Body = {event.get_body().decode()}')
  logging.info(f'----------------------------------------')
  logging.info(f'B={B}')
  logging.info(f'C={C}')
  logging.info(f'########################################')

带有输出bindings and func.unc.unc.ut and func.ut and func.out tymed tym the函数,azure,azure功能不会触发。没有,确实如此。

任何帮助将不胜感激。

I am trying to setup an azure function to receive messages from EventHub "A", then send messages to one of eventHub "B" or C depending on the input message using function bindings.

Azure functions supports both trigger and output bindings for the EventHub, however, i can't figure out how to setup imperative output bindings properly.

Here are my function.json and python function code so far, inspired by the docs:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "event",
      "type": "eventHubTrigger",
      "direction": "in",
      "eventHubName": "A",
      "consumerGroup": "myConsumerGroup",
      "connection": "aConnectionString"
    },
    {
      "type": "eventHub",
      "name": "B",
      "eventHubName": "B",
      "connection": "bConnectionString",
      "direction": "out"
    },
    {
      "type": "eventHub",
      "name": "C",
      "eventHubname": "C",
      "connection": "cConnectionString",
      "direction": "out"
    }
  ]
}
import logging
import azure.functions as func

def main(event: func.EventHubEvent, B: func.Out, C: func.Out):
  logging.info(f'########################################')
  logging.info(f'Function triggered to process a message:')
  logging.info(f'  EnqueuedTimeUtc = {event.enqueued_time}')
  logging.info(f'  SequenceNumber = {event.sequence_number}')
  logging.info(f'  Offset = {event.offset}')
  logging.info(f'----------------------------------------')
  logging.info(f'  Metadata: ')
  for key in event.metadata:
    logging.info(f'    key {key} = {event.metadata[key]}')
  logging.info(f'  Body = {event.get_body().decode()}')
  logging.info(f'----------------------------------------')
  logging.info(f'B={B}')
  logging.info(f'C={C}')
  logging.info(f'########################################')

With the output bindings and func.Out typed params in the function, the azure function doesn't trigger. Without, it does.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

圈圈圆圆圈圈 2025-02-17 07:58:56

我使用Azurite Vscode扩展名来弄清楚它:有几个问题:

  • b的输出绑定名称中有一个下划线。这使得名字无效。
  • func.out是一种通用类型,需要注释类型:例如:func.out [str]

I figured it out using the Azurite vscode extension: there were several issues:

  • the output binding name for B had an underscore in it. that makes the name invalid.
  • func.Out is a generic type, and the type needs to be annotated: eg: func.Out[str]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文