This document explains how to use the Event Hub trigger for Azure Functions. The Event Hub trigger allows your Azure Functions to react to events published to an Azure Event Hubs instance.
The Event Hub trigger integrates seamlessly with Azure Event Hubs, a highly scalable data streaming platform and event ingestion service. When events are sent to an Event Hub, the trigger can automatically invoke your function to process these events.
The Event Hub trigger is configured using attributes in your function code or through the function.json file.
C#
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Azure.Messaging.EventHubs;
using System.Collections.Generic;
using System.Threading.Tasks;
public static class EventHubProcessor
{
    [FunctionName("EventHubTriggerFunction")]
    public static async Task Run(
        [EventHubTrigger("my-event-hub", Connection = "EventHubConnectionAppSetting")] EventData[] events,
        ILogger log)
    {
        log.LogInformation($"C# Event Hub trigger function processed a batch of {events.Length} events.");
        foreach (EventData eventData in events)
        {
            log.LogInformation($"Message Body: {System.Text.Encoding.UTF8.GetString(eventData.EventBody.ToArray())}");
            log.LogInformation($"Enqueued Time Utc: {eventData.EnqueuedTime}");
            log.LogInformation($"Partition Key: {eventData.PartitionKey}");
        }
        log.LogInformation("Finished processing events.");
    }
}
        function.json)In your function.json file for a JavaScript function:
JSON
{
  "bindings": [
    {
      "name": "eventHubMessages",
      "type": "eventHubTrigger",
      "direction": "in",
      "eventHubName": "my-event-hub",
      "connection": "EventHubConnectionAppSetting",
      "consumerGroup": "$Default"
    }
  ],
  "scriptFile": "index.js"
}
        And in your index.js file:
JavaScript
module.exports = async function (context, eventHubMessages) {
    context.log(`JavaScript Event Hub trigger function processed a batch of ${eventHubMessages.length} events.`);
    for (const eventData of eventHubMessages) {
        context.log(`Message Body: ${eventData.body}`);
        context.log(`Enqueued Time Utc: ${eventData.enqueuedTimeUtc}`);
        context.log(`Partition Key: ${eventData.partitionKey}`);
    }
    context.log("Finished processing events.");
};
        | Property | Description | Required | 
|---|---|---|
| eventHubName | The name of the Event Hub to monitor. | Yes | 
| connection | The name of an App Setting that contains the Event Hubs connection string. | Yes | 
| consumerGroup | The consumer group that the function will join. Defaults to $Default. | No | 
| accessRights | The access rights required for the trigger. Defaults to Listen. | No | 
connection property is properly configured in your Azure Functions app settings with the correct Event Hubs connection string.
        The Event Hub trigger processes events in batches. The size of the batch can be configured. This allows for more efficient processing of large volumes of events.
You can configure the batch size using the IsBatched and MaxBatchSize properties in function.json or attributes in C#.
JSON
{
  "bindings": [
    {
      "name": "eventHubMessages",
      "type": "eventHubTrigger",
      "direction": "in",
      "eventHubName": "my-event-hub",
      "connection": "EventHubConnectionAppSetting",
      "consumerGroup": "$Default",
      "isBatched": true,
      "maxBatchSize": 100
    }
  ],
  "scriptFile": "index.js"
}
        C#
[EventHubTrigger("my-event-hub", Connection = "EventHubConnectionAppSetting", IsBatched = true, MaxBatchSize = 100)]
public static async Task Run(EventData[] events, ILogger log) { ... }
        isBatched is true, your function receives an array of EventData objects (or equivalent in other languages). If isBatched is false, the function receives a single event.
        Azure Functions provides built-in mechanisms for handling errors and retrying failed operations. The Event Hub trigger leverages these by default. If an error occurs during function execution, the batch of events might be retried.
Consider implementing idempotent logic within your function to safely handle potential retries without unintended side effects.
The Event Hub trigger automatically manages checkpoints to keep track of which events have been successfully processed. This ensures that events are not lost and that processing can resume from the last checkpoint in case of failures or restarts.
The checkpointing mechanism is typically managed by the Azure Functions runtime based on the consumer group and Event Hub configuration.
For more advanced scenarios and detailed API references, please refer to the official Azure Functions documentation.