azure-sdk-for-cpp/sdk/eventhubs/azure-messaging-eventhubs/README.md
Ronnie Geraghty b391d65809
Docs links coversion: docs.microsoft.com -> learn.microsoft.com (#6276)
* Docs links coversion: docs.microsoft.com -> learn.microsoft.com
Some markdown formatting

* Adding "msrc" to cspell words

* Undoing things auto formatter did

* Appling Ahson's suggestions
2024-12-16 09:32:19 -08:00

11 KiB

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:

az login

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>

// Your Event Hubs namespace connection string is available in the Azure portal.
std::string connectionString = "<connection_string>";
std::string eventHubName = "<event_hub_name>";

Azure::Messaging::EventHubs::EventDataBatchOptions batchOptions;
batchOptions.PartitionId = "1";
Azure::Messaging::EventHubs::EventDataBatch eventBatch(batchOptions);

Azure::Messaging::EventHubs::Models::EventData message;
message.Body.Data = {'H', 'e', 'l', 'l', 'o', '2'};

eventBatch.AddMessage(message);

Azure::Messaging::EventHubs::ProducerClientOptions producerOptions;
producerOptions.Name = "sender-link";
producerOptions.ApplicationID = "some";

auto client = Azure::Messaging::EventHubs::ProducerClient(
      connectionString, eventHubName, producerOptions);
auto result = client.SendEventDataBatch(eventBatch);

Receive events

The following example shows how to receive events from partition 1 on an event hub:

#include <azure/messaging/eventhubs.hpp>


// Your Event Hubs namespace connection string is available in the Azure portal.
std::string connectionString = "<connection_string>";
std::string eventHubName = "<event_hub_name>";

auto client = Azure::Messaging::EventHubs::ConsumerClient(
	connectionString, eventHubName);

Azure::Messaging::EventHubs::PartitionClient partitionClient
        = client.CreatePartitionClient("1");

auto events = partitionClient.ReceiveEvents(1);

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.

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) secure@microsoft.com. 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.

Impressions