Azure Event Hubs Client Package for C++
Azure Event Hubs is a big data streaming platform and event ingestion service from Microsoft. For more information about Event Hubs see: link.
Use the client library github.com/Azure/azure-sdk-for-cpp/sdk/eventhubs
in your application to:
- Send events to an event hub.
- Consume events from an event hub.
Key links:
Getting started
Install the package
Install the Azure Event Hubs client package for C++ with vcpkg
:
vcpkg install azure-messaging-eventhubs-cpp
Prerequisites
Create a namespace using the Azure CLI
Login to the CLI:
Create a resource group:
az group create --name <your group name> --location <your location> --subscription <your subscription>
This should output something like:
{
"id": "/subscriptions/<your subscription ID>/resourceGroups/<your group name>",
"location": "<your location>",
"managedBy": null,
"name": "<yourgroup name>",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null,
"type": "Microsoft.Resources/resourceGroups"
}
Create an EventHubs namespace:
az eventhubs namespace create --resource-group <your group name> --name <your namespace name> --sku Standard --subscription <your subscription>
This should output something like:
{
"createdAt": "2023-08-10T18:41:54.19Z",
"disableLocalAuth": false,
"id": "/subscriptions/<your subscription ID>/resourceGroups/<your group name>/providers/Microsoft.EventHub/namespaces/<your namespace>",
"isAutoInflateEnabled": false,
"kafkaEnabled": true,
"location": "West US",
"maximumThroughputUnits": 0,
"metricId": "REDACTED",
"minimumTlsVersion": "1.2",
"name": "<your namespace name>",
"provisioningState": "Succeeded",
"publicNetworkAccess": "Enabled",
"resourceGroup": "<your resource group>",
"serviceBusEndpoint": "https://<your namespace name>.servicebus.windows.net:443/",
"sku": {
"capacity": 1,
"name": "Standard",
"tier": "Standard"
},
"status": "Active",
"tags": {},
"type": "Microsoft.EventHub/Namespaces",
"updatedAt": "2023-08-10T18:42:41.343Z",
"zoneRedundant": false
}
Create an EventHub:
az eventhubs eventhub create --resource-group <your resource group> --namespace-name <your namespace name> --name <your eventhub name>
That should output something like:
{
"createdAt": "2023-08-10T21:02:07.62Z",
"id": "/subscriptions/<your subscription>/resourceGroups/<your group name>/providers/Microsoft.EventHub/namespaces/<your namespace name>/eventhubs/<your eventhub name>",
"location": "westus",
"messageRetentionInDays": 7,
"name": "<your eventhub name>",
"partitionCount": 4,
"partitionIds": [
"0",
"1",
"2",
"3"
],
"resourceGroup": "<your group name>",
"retentionDescription": {
"cleanupPolicy": "Delete",
"retentionTimeInHours": 168
},
"status": "Active",
"type": "Microsoft.EventHub/namespaces/eventhubs",
"updatedAt": "2023-08-10T21:02:16.29Z"
}
Authenticate the client
Event Hub clients are created using a credential from the Azure Identity package, like DefaultAzureCredential. Alternatively, you can create a client using a connection string.
Using a service principal
- ConsumerClient: link
- ProducerClient: link
Using a connection string
- ConsumerClient: link
- ProducerClient: link
Key concepts
An Event Hub namespace can have multiple event hubs. Each event hub, in turn, contains partitions which store events.
Events are published to an event hub using an event publisher. In this package, the event publisher is the ProducerClient
Events can be consumed from an event hub using an event consumer. In this package there are two types for consuming events:
- The basic event consumer is the PartitionClient, in the ConsumerClient. This consumer is useful if you already known which partitions you want to receive from.
- A distributed event consumer, which uses Azure Blobs for checkpointing and coordination. This is implemented in the Processor. The Processor is useful when you want to have the partition assignment be dynamically chosen, and balanced with other Processor instances.
More information about Event Hubs features and terminology can be found here: link
Examples
Examples for various scenarios can be found on azure.github.io or in the samples directory in our GitHub repo for EventHubs.
Send events
The following example shows how to send events to an event hub:
#include <azure/messaging/eventhubs.hpp>
std::string connectionString = "<connection_string>";
std::string eventHubName = "<event_hub_name>";
message.
Body.Data = {
'H',
'e',
'l',
'l',
'o',
'2'};
eventBatch.AddMessage(message);
producerOptions.
Name =
"sender-link";
connectionString, eventHubName, producerOptions);
auto result = client.SendEventDataBatch(eventBatch);
EventDataBatch is used to efficiently pack up EventData before sending it to Event Hubs.
Definition event_data_batch.hpp:54
Represents an event sent to the Azure Event Hubs service.
Definition event_data.hpp:19
std::vector< uint8_t > Body
The body of the event data.
Definition event_data.hpp:23
ProducerClient can be used to send events to an Event Hub.
Definition producer_client.hpp:73
EventDataBatchOptions contains optional parameters for the [ProducerClient.CreateEventDataBatch] func...
Definition event_data_batch.hpp:29
std::string PartitionId
PartitionId is the ID of the partition to send these messages to. Note that if you use this option th...
Definition event_data_batch.hpp:45
Contains options for the ProducerClient creation.
Definition producer_client.hpp:28
std::string Name
The name of the producer client link, used in diagnostics.
Definition producer_client.hpp:40
std::string ApplicationID
Application ID that will be passed to the namespace.
Definition producer_client.hpp:31
Receive events
The following example shows how to receive events from partition 1 on an event hub:
#include <azure/messaging/eventhubs.hpp>
std::string connectionString = "<connection_string>";
std::string eventHubName = "<event_hub_name>";
connectionString, eventHubName);
= client.CreatePartitionClient("1");
The ConsumerClient class is a high level class used to consume events from an Event Hub.
Definition consumer_client.hpp:78
Definition partition_client.hpp:58
std::vector< std::shared_ptr< const Models::ReceivedEventData > > ReceiveEvents(uint32_t maxMessages, Core::Context const &context={})
Definition partition_client.cpp:188
Troubleshooting
Logging
The EventHubs SDK client uses the Azure SDK log message functionality to enable diagnostics.
Contributing
For details on contributing to this repository, see the contributing guide.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Additional Helpful Links for Contributors
Many people all over the world have helped make this project better. You'll want to check out:
Reporting security issues and security bugs
Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) secur.nosp@m.e@mi.nosp@m.croso.nosp@m.ft.c.nosp@m.om. You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Further information, including the MSRC PGP key, can be found in the Security TechCenter.
License
Azure SDK for C++ is licensed under the MIT license.