Azure Functions Event Hubs Bindings

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.

Event Hubs Trigger

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.

Trigger Configuration

You can configure the Event Hubs trigger by defining a function.json file (for JavaScript, Python, etc.) or using attributes (for C#, Java).

Example (function.json)

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "myEventHubTrigger",
      "type": "eventHubTrigger",
      "direction": "in",
      "eventHubName": "myeventhub",
      "connection": "EventHubConnection"
    }
  ]
}

Example (C# Attributes)

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())}");
        }
    }
}
Note: The Connection setting refers to an application setting that contains the Event Hubs connection string.

Event Data Structure

The trigger provides an array of events. The structure of each event typically includes:

Event Hubs Output Binding

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.

Output Binding Configuration

Example (function.json)

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "outputEventHub",
      "type": "eventHub",
      "direction": "out",
      "eventHubName": "outputeventhub",
      "connection": "EventHubConnection"
    }
  ]
}

Example (C# Attributes)

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.");
    }
}

Common Scenarios

Key Configuration Options

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.

Best Practices

For more detailed information, please refer to the official Azure Functions Event Hubs documentation.