适用于 Azure Functions 的 Azure 事件中心输出绑定Azure Event Hubs output binding for Azure Functions

本文介绍如何使用 Azure Functions 的 Azure 事件中心绑定。This article explains how to work with Azure Event Hubs bindings for Azure Functions. Azure Functions 支持事件中心的触发器和输出绑定。Azure Functions supports trigger and output bindings for Event Hubs.

有关设置和配置详细信息,请参阅概述For information on setup and configuration details, see the overview.

使用事件中心输出绑定将事件写入到事件流。Use the Event Hubs output binding to write events to an event stream. 必须具有事件中心的发送权限才可将事件写入到其中。You must have send permission to an event hub to write events to it.

在尝试实现输出绑定之前,请确保所需的包引用已准备就绪。Make sure the required package references are in place before you try to implement an output binding.

以下示例演示使用方法返回值作为输出将消息写入到事件中心的 C# 函数The following example shows a C# function that writes a message to an event hub, using the method return value as the output:

[FunctionName("EventHubOutput")]
[return: EventHub("outputEventHubMessage", Connection = "EventHubConnectionAppSetting")]
public static string Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    return $"{DateTime.Now}";
}

以下示例演示如何使用 IAsyncCollector 接口发送一批消息。The following example shows how to use the IAsyncCollector interface to send a batch of messages. 当你处理来自一个事件中心的消息并将结果发送到另一个事件中心时,这种情况很常见。This scenario is common when you are processing messages coming from one Event Hub and sending the result to another Event Hub.

[FunctionName("EH2EH")]
public static async Task Run(
    [EventHubTrigger("source", Connection = "EventHubConnectionAppSetting")] EventData[] events,
    [EventHub("dest", Connection = "EventHubConnectionAppSetting")]IAsyncCollector<string> outputEvents,
    ILogger log)
{
    foreach (EventData eventData in events)
    {
        // do some processing:
        var myProcessedEvent = DoSomething(eventData);

        // then send the message
        await outputEvents.AddAsync(JsonConvert.SerializeObject(myProcessedEvent));
    }
}

特性和注释Attributes and annotations

对于 C# 类库,使用 EventHubAttribute 特性。For C# class libraries, use the EventHubAttribute attribute.

该特性的构造函数使用事件中心的名称和包含连接字符串的应用设置的名称。The attribute's constructor takes the name of the event hub and the name of an app setting that contains the connection string. 有关这些设置的详细信息,请参阅输出 - 配置For more information about these settings, see Output - configuration. 下面是 EventHub 特性的示例:Here's an EventHub attribute example:

[FunctionName("EventHubOutput")]
[return: EventHub("outputEventHubMessage", Connection = "EventHubConnectionAppSetting")]
public static string Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
{
    ...
}

有关完整示例,请参阅输出 - C# 示例For a complete example, see Output - C# example.

配置Configuration

下表解释了在 function.json 文件和 EventHub 特性中设置的绑定配置属性。The following table explains the binding configuration properties that you set in the function.json file and the EventHub attribute.

function.json 属性function.json property Attribute 属性Attribute property 说明Description
typetype 不适用n/a 必须设置为“eventHub”。Must be set to "eventHub".
directiondirection 不适用n/a 必须设置为“out”。Must be set to "out". 在 Azure 门户中创建绑定时,会自动设置该参数。This parameter is set automatically when you create the binding in the Azure portal.
namename 不适用n/a 函数代码中使用的表示事件的变量名称。The variable name used in function code that represents the event.
路径path EventHubNameEventHubName 仅适用于 Functions 1.x。Functions 1.x only. 事件中心的名称。The name of the event hub. 当事件中心名称也出现在连接字符串中时,该值会在运行时覆盖此属性。When the event hub name is also present in the connection string, that value overrides this property at runtime.
eventHubNameeventHubName EventHubNameEventHubName Functions 2.x 及更高版本。Functions 2.x and higher. 事件中心的名称。The name of the event hub. 当事件中心名称也出现在连接字符串中时,该值会在运行时覆盖此属性。When the event hub name is also present in the connection string, that value overrides this property at runtime.
连接connection ConnectionConnection 应用设置的名称,该名称中包含事件中心命名空间的连接字符串。The name of an app setting that contains the connection string to the event hub's namespace. 单击 命名空间 (而不是事件中心本身)的“连接信息”按钮,以复制此连接字符串。Copy this connection string by clicking the Connection Information button for the namespace, not the event hub itself. 此连接字符串必须具有发送权限才可将消息发送到事件流。This connection string must have send permissions to send the message to the event stream.

在本地进行开发时,应用设置将取 local.settings.json 文件的值。When you're developing locally, app settings go into the local.settings.json file.

使用情况Usage

使用 out string paramName 等方法参数发送消息。Send messages by using a method parameter such as out string paramName. 在 C# 脚本中,paramName 是在 function.jsonname 属性中指定的值。In C# script, paramName is the value specified in the name property of function.json. 若要编写多条消息,可以使用 ICollector<string>IAsyncCollector<string> 代替 out stringTo write multiple messages, you can use ICollector<string> or IAsyncCollector<string> in place of out string.

异常和返回代码Exceptions and return codes

绑定Binding 参考Reference
事件中心Event Hub 操作指南Operations Guide

后续步骤Next steps