Module azure_eventhub
![](https://bcentral-packageicons.azureedge.net/images/ballerinax_azure_eventhub_2.1.0.png)
ballerinax/azure_eventhub Ballerina library
Overview
The Ballerina connector for Azure Event Hub provides access to the Azure Event Hub via the Ballerina Language. This connector allows you to ingest, buffer, store, and process high-frequency data from any source in real-time to derive important business insights using the Azure Event Hub. It provides the capability to perform Event Hub service operations, Event Hub management operations, handle publisher policy operations.
This module supports Azure Event Hub REST API 2014-01 version.
Prerequisites
Before using this connector in your Ballerina application, complete the following:
-
Obtain tokens
Shared Access Signature (SAS) authentication credentials are required to communicate with the Event Hub. These credentials are available in the connection string of the Event Hub namespace.
-
Obtain the connection string for the Event Hub namespace by following the instructions given here. (Eg:
"Endpoint=sb://<Namespace_Name>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=i1f2BXAbmtxhu............f9GMyfnNWvQ3CY="
) -
Extract the shared access key name, shared access key, and resource URI of the Event Hub namespace from the connection string.
- Shared access key name (Eg:
"RootManageSharedAccessKey"
) - Shared access key (Eg:
"i1f2BXAbmtxhu............f9GMyfnNWvQ3CY="
) - Resource URI to the Event Hub namespace (Eg:
"<Namespace_Name>.servicebus.windows.net"
)
- Shared access key name (Eg:
-
Quickstart
To use the Azure Event Hub connector in your Ballerina application, update the .bal file as follows:
Step 1: Import connector
Import the ballerinax/azure_eventhub
module into the Ballerina project.
import ballerinax/azure_eventhub;
Step 2: Create a new connector instance
Create an azure_eventhub:ConnectionConfig
with the extracted shared access key name, shared access key,
the resource uri and initialize the connector with it.
azure_eventhub:ConnectionConfig config = { sasKeyName: <SAS_KEY_NAME>, sasKey: <SAS_KEY>, resourceUri: <RESOURCE_URI> }; azure_eventhub:Client eventHubClient = check new (config);
Step 3: Invoke connector operation
-
Now you can use the operations available within the connector. Note that they are in the form of remote operations.
Following is an example on how to send an event to the Azure Event Hub using the connector.
Send an event to the Azure Event Hub
public function main() returns error? { map<string> brokerProps = {CorrelationId: "34", CorrelationId2: "83"}; map<string> userProps = {Alert: "windy", warning: "true"}; check eventHubClient->send(eventHubPath, eventData, userProps, brokerProps, partitionKey = "groupName"); log:printInfo("Successfully Send Event to Event Hub!"); }
-
Use
bal run
command to compile and run the Ballerina program.