author | ms.service | ms.topic | ms.date | ms.author |
---|---|---|---|---|
craigshoemaker |
azure-functions |
include |
02/21/2020 |
cshoe |
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.
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}";
}
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));
}
}
The following example shows an event hub trigger binding in a function.json file and a C# script function that uses the binding. The function writes a message to an event hub.
The following examples show Event Hubs binding data in the function.json file. The first example is for Functions 2.x and higher, and the second one is for Functions 1.x.
{
"type": "eventHub",
"name": "outputEventHubMessage",
"eventHubName": "myeventhub",
"connection": "MyEventHubSendAppSetting",
"direction": "out"
}
{
"type": "eventHub",
"name": "outputEventHubMessage",
"path": "myeventhub",
"connection": "MyEventHubSendAppSetting",
"direction": "out"
}
Here's C# script code that creates one message:
using System;
using Microsoft.Extensions.Logging;
public static void Run(TimerInfo myTimer, out string outputEventHubMessage, ILogger log)
{
String msg = $"TimerTriggerCSharp1 executed at: {DateTime.Now}";
log.LogInformation(msg);
outputEventHubMessage = msg;
}
Here's C# script code that creates multiple messages:
public static void Run(TimerInfo myTimer, ICollector<string> outputEventHubMessage, ILogger log)
{
string message = $"Message created at: {DateTime.Now}";
log.LogInformation(message);
outputEventHubMessage.Add("1 " + message);
outputEventHubMessage.Add("2 " + message);
}
The following example shows an event hub trigger binding in a function.json file and a JavaScript function that uses the binding. The function writes a message to an event hub.
The following examples show Event Hubs binding data in the function.json file. The first example is for Functions 2.x and higher, and the second one is for Functions 1.x.
{
"type": "eventHub",
"name": "outputEventHubMessage",
"eventHubName": "myeventhub",
"connection": "MyEventHubSendAppSetting",
"direction": "out"
}
{
"type": "eventHub",
"name": "outputEventHubMessage",
"path": "myeventhub",
"connection": "MyEventHubSendAppSetting",
"direction": "out"
}
Here's JavaScript code that sends a single message:
module.exports = function (context, myTimer) {
var timeStamp = new Date().toISOString();
context.log('Message created at: ', timeStamp);
context.bindings.outputEventHubMessage = "Message created at: " + timeStamp;
context.done();
};
Here's JavaScript code that sends multiple messages:
module.exports = function(context) {
var timeStamp = new Date().toISOString();
var message = 'Message created at: ' + timeStamp;
context.bindings.outputEventHubMessage = [];
context.bindings.outputEventHubMessage.push("1 " + message);
context.bindings.outputEventHubMessage.push("2 " + message);
context.done();
};
The following example shows an event hub trigger binding in a function.json file and a Python function that uses the binding. The function writes a message to an event hub.
The following examples show Event Hubs binding data in the function.json file.
{
"type": "eventHub",
"name": "$return",
"eventHubName": "myeventhub",
"connection": "MyEventHubSendAppSetting",
"direction": "out"
}
Here's Python code that sends a single message:
import datetime
import logging
import azure.functions as func
def main(timer: func.TimerRequest) -> str:
timestamp = datetime.datetime.utcnow()
logging.info('Message created at: %s', timestamp)
return 'Message created at: {}'.format(timestamp)
The following example shows a Java function that writes a message containing the current time to an Event Hub.
@FunctionName("sendTime")
@EventHubOutput(name = "event", eventHubName = "samples-workitems", connection = "AzureEventHubConnection")
public String sendTime(
@TimerTrigger(name = "sendTimeTrigger", schedule = "0 */5 * * * *") String timerInfo) {
return LocalDateTime.now().toString();
}
In the Java functions runtime library, use the @EventHubOutput
annotation on parameters whose value would be published to Event Hub. The parameter should be of type OutputBinding<T>
, where T is a POJO or any native Java type.
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. Here's an EventHub
attribute example:
[FunctionName("EventHubOutput")]
[return: EventHub("outputEventHubMessage", Connection = "EventHubConnectionAppSetting")]
public static string Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
{
...
}
For a complete example, see Output - C# example.
Attributes are not supported by C# Script.
Attributes are not supported by JavaScript.
Attributes are not supported by Python.
In the Java functions runtime library, use the EventHubOutput annotation on parameters whose value would be published to Event Hub. The parameter should be of type OutputBinding<T>
, where T
is a POJO or any native Java type.
The following table explains the binding configuration properties that you set in the function.json file and the EventHub
attribute.
function.json property | Attribute property | Description |
---|---|---|
type | n/a | Must be set to "eventHub". |
direction | n/a | Must be set to "out". This parameter is set automatically when you create the binding in the Azure portal. |
name | n/a | The variable name used in function code that represents the event. |
path | EventHubName | 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. |
eventHubName | EventHubName | 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 | Connection | 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. If you are using version 5.x or higher of the extension, instead of a connection string, you can provide a reference to a configuration section which defines the connection. See Connections. |
[!INCLUDE app settings to local.settings.json]
You can use the following parameter types for the output binding of Event Hub:
string
byte[]
POCO
EventData
- The default properties of EventData are provided in the for the Microsoft.Azure.EventHubs namespace.
Send messages by using a method parameter such as out string paramName
. In C# script, paramName
is the value specified in the name
property of function.json. To write multiple messages, you can use ICollector<string>
or
IAsyncCollector<string>
in place of out string
.
Apps using the 5.0.0 or higher version of the Event Hub extension use the EventData
type in Azure.Messaging.EventHubs instead of the one in Microsoft.Azure.EventHubs namespace. This version drops support for the legacy Body
type in favor of the following types:
You can use the following parameter types for the output binding of Event Hub:
string
byte[]
POCO
EventData
- The default properties of EventData are provided in the for the Microsoft.Azure.EventHubs namespace.
Send messages by using a method parameter such as out string paramName
. In C# script, paramName
is the value specified in the name
property of function.json. To write multiple messages, you can use ICollector<string>
or
IAsyncCollector<string>
in place of out string
.
Apps using the 5.0.0 or higher version of the Event Hub extension use the EventData
type in Azure.Messaging.EventHubs instead of the one in Microsoft.Azure.EventHubs namespace. This version drops support for the legacy Body
type in favor of the following types:
Access the output event by using context.bindings.<name>
where <name>
is the value specified in the name
property of function.json.
There are two options for outputting an Event Hub message from a function:
-
Return value: Set the
name
property in function.json to$return
. With this configuration, the function's return value is persisted as an Event Hub message. -
Imperative: Pass a value to the set method of the parameter declared as an Out type. The value passed to
set
is persisted as an Event Hub message.
There are two options for outputting an Event Hub message from a function by using the EventHubOutput annotation:
-
Return value: By applying the annotation to the function itself, the return value of the function is persisted as an Event Hub message.
-
Imperative: To explicitly set the message value, apply the annotation to a specific parameter of the type
OutputBinding<T>
, whereT
is a POJO or any native Java type. With this configuration, passing a value to thesetValue
method persists the value as an Event Hub message.
Binding | Reference |
---|---|
Event Hub | Operations Guide |