This document describes how to use Event Hubs triggers and bindings with Azure Functions. Event Hubs is a big data streaming platform and event ingestion service. Azure Functions provides built-in triggers and bindings for Event Hubs, enabling you to easily process events from Event Hubs.
The Event Hubs trigger allows your function to be executed automatically in response to new events arriving in an Event Hub. It supports batching of events for efficient processing.
You can configure the Event Hubs trigger by defining a function.json file (for JavaScript, Python, etc.) or using attributes (for C#, Java).
{
"scriptFile": "index.js",
"bindings": [
{
"name": "myEventHubTrigger",
"type": "eventHubTrigger",
"direction": "in",
"eventHubName": "myeventhub",
"connection": "EventHubConnection"
}
]
}
using Microsoft.Azure.WebJobs;
public static class EventHubProcessor
{
[FunctionName("EventHubProcessorFunction")]
public static void Run(
[EventHubTrigger("myeventhub", Connection = "EventHubConnection")] EventData[] events,
ILogger log)
{
log.LogInformation($"C# Event Hub trigger function processed a batch of {events.Length} events.");
foreach (var eventData in events)
{
log.LogInformation($"Processing event: {Encoding.UTF8.GetString(eventData.Body.ToArray())}");
}
}
}
Connection setting refers to an application setting that contains the Event Hubs connection string.
The trigger provides an array of events. The structure of each event typically includes:
Body: The actual event data.Properties: Custom metadata associated with the event.SystemProperties: System-generated properties like sequence number and offset.The Event Hubs output binding allows your function to send events to an Event Hub. This is useful for scenarios where your function processes incoming data and needs to forward it or transform it before sending it to another destination.
{
"scriptFile": "index.js",
"bindings": [
{
"name": "outputEventHub",
"type": "eventHub",
"direction": "out",
"eventHubName": "outputeventhub",
"connection": "EventHubConnection"
}
]
}
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.EventHubs;
public static class EventHubSender
{
[FunctionName("EventHubSenderFunction")]
public static void Run(
[TimerTrigger("0 */5 * * * *")]TimerInfo myTimer,
[EventHub("outputeventhub", Connection = "EventHubConnection")] out EventData[] outputEvents,
ILogger log)
{
log.LogInformation("Sending events to Event Hub...");
outputEvents = new EventData[]
{
new EventData(Encoding.UTF8.GetBytes("Hello from Azure Functions - " + DateTime.Now.ToString())),
new EventData(Encoding.UTF8.GetBytes("Another event")),
};
log.LogInformation("Events sent successfully.");
}
}
| Parameter | Description |
|---|---|
eventHubName |
The name of the Event Hub to bind to. |
connection |
The name of the application setting containing the Event Hubs connection string. |
consumerGroup (Trigger) |
The consumer group name to use for the Event Hub trigger. Defaults to $Default. |
cardinality (Trigger) |
Defines whether the trigger receives single events or batches. Can be one or many. Defaults to many. |
partitionId (Trigger) |
Specifies a particular partition to receive events from. |
$Default consumer group for general processing, but create dedicated consumer groups for specific applications or services.For more detailed information, please refer to the official Azure Functions Event Hubs documentation.