Crates.io | azure_messaging_eventhubs |
lib.rs | azure_messaging_eventhubs |
version | |
source | src |
created_at | 2025-02-19 00:18:05.918597+00 |
updated_at | 2025-04-09 23:33:59.911615+00 |
description | Rust client for Azure Eventhubs Service |
homepage | https://github.com/azure/azure-sdk-for-rust |
repository | https://github.com/azure/azure-sdk-for-rust |
max_upload_size | |
id | 1560743 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
Azure Event Hubs is a big data streaming platform and event ingestion service from Microsoft. For more information about Event Hubs see: link.
The Azure Event Hubs client library allows you to send single events or batches of events to an event hub and consume events from an event hub.
Source code | Package (crates.io) | API reference documentation | Product documentation
Install the Azure Event Hubs client library for Rust with Cargo:
cargo add azure_messaging_eventhubs
If you use the Azure CLI, replace <your-resource-group-name>
, <your-eventhubs-namespace-name>
, and <your-eventhub-name>
with your own, unique names:
Create an Event Hubs Namespace:
az eventhubs namespace create --resource-group <your-resource-group-name> --name <your-eventhubs-namespace-name> --sku Standard
Create an Event Hub Instance:
az eventhubs eventhub create --resource-group <your-resource-group-name> --namespace-name <your-eventhubs-namespace-name> --name <your-eventhub-name>
Add the following crates to your project:
cargo add azure_identity tokio
In order to interact with the Azure Event Hubs service, you'll need to create an instance of the ProducerClient
or the ConsumerClient
. You need an event hub namespace host URL (which you may see as serviceBusEndpoint
in the Azure CLI response when creating the Even Hubs Namespace), an Event Hub name (which you may see as name
in the Azure CLI response when crating the Event Hub instance), and credentials to instantiate a client object.
The example shown below uses a DefaultAzureCredential
, which is appropriate for most local development environments. Additionally, we recommend using a managed identity for authentication in production environments. You can find more information on different ways of authenticating and their corresponding credential types in the Azure Identity documentation.
The DefaultAzureCredential
will automatically pick up on an Azure CLI authentication. Ensure you are logged in with the Azure CLI:
az login
Instantiate a DefaultAzureCredential
to pass to the client. The same instance of a token credential can be used with multiple clients if they will be authenticating with the same identity.
use azure_identity::DefaultAzureCredential;
use azure_messaging_eventhubs::ProducerClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let host = "<EVENTHUBS_HOST>".to_string();
let eventhub = "<EVENTHUB_NAME>".to_string();
// Create new credential
let credential = DefaultAzureCredential::new()?;
// Create and open a new ProducerClient
let producer = ProducerClient::builder()
.open(host, eventhub, credential.clone())
.await?;
producer.send_event(vec![1, 2, 3, 4], None).await?;
Ok(())
}
An Event Hub namespace can have multiple Event Hub instances. Each Event Hub instance, in turn, contains partitions which store events.
Events are published to an Event Hub instance using an event publisher. In this package, the event publisher is the ProducerClient
Events can be consumed from an Event Hub instance using an event consumer.
Consuming events is done using an EventReceiver
, which can be opened from the ConsumerClient
. This is useful if you already known which partitions you want to receive from.
More information about Event Hubs features and terminology can be found here: link
Additional examples for various scenarios can be found on in the examples directory in our GitHub repo for Event Hubs.
use azure_core::Error;
use azure_identity::DefaultAzureCredential;
use azure_messaging_eventhubs::ProducerClient;
async fn open_producer_client() -> Result<ProducerClient, Error> {
let host = "<EVENTHUBS_HOST>".to_string();
let eventhub = "<EVENTHUB_NAME>".to_string();
let credential = DefaultAzureCredential::new()?;
let producer = ProducerClient::builder()
.open(host, eventhub, credential.clone())
.await?;
Ok(producer)
}
There are two mechanisms used to send events to an Event Hub instance. The first directly sends individual messages to the Event Hub, the second uses a "batch" operation to send multiple messages in a single network request to the service.
use azure_core::Error;
use azure_messaging_eventhubs::ProducerClient;
async fn send_events(producer: &ProducerClient) -> Result<(), Error> {
producer.send_event(vec![1, 2, 3, 4], None).await?;
Ok(())
}
use azure_core::Error;
use azure_messaging_eventhubs::ProducerClient;
async fn send_events(producer: &ProducerClient) -> Result<(), Error> {
let batch = producer.create_batch(None).await?;
assert_eq!(batch.len(), 0);
assert!(batch.try_add_event_data(vec![1, 2, 3, 4], None)?);
let res = producer.send_batch(&batch, None).await;
assert!(res.is_ok());
Ok(())
}
use azure_core::Error;
use azure_identity::DefaultAzureCredential;
use azure_messaging_eventhubs::ConsumerClient;
async fn open_consumer_client() -> Result<ConsumerClient, Error> {
let host = "<EVENTHUBS_HOST>".to_string();
let eventhub = "<EVENTHUB_NAME>".to_string();
let credential = DefaultAzureCredential::new()?;
let consumer = azure_messaging_eventhubs::ConsumerClient::builder()
.open(host, eventhub, credential.clone())
.await?;
Ok(consumer)
}
The following example shows how to receive events from partition 0 on an Event Hubs instance.
It assumes that the caller has provided a consumer client which will be used to receive events.
Each message receiver can only receive messages from a single Event Hubs partition
use futures::stream::StreamExt;
use azure_core::Error;
use azure_messaging_eventhubs::{
ConsumerClient, OpenReceiverOptions, StartLocation, StartPosition,
};
// By default, an event receiver only receives new events from the event hub. To receive events from earlier, specify
// a `start_position` which represents the position from which to start receiving events.
// In this example, events are received from the start of the partition.
async fn receive_events(client: &ConsumerClient) -> Result<(), Error> {
let message_receiver = client
.open_receiver_on_partition(
"0".to_string(),
Some(OpenReceiverOptions {
start_position: Some(StartPosition {
location: StartLocation::Earliest,
..Default::default()
}),
..Default::default()
}),
)
.await?;
let mut event_stream = message_receiver.stream_events();
while let Some(event_result) = event_stream.next().await {
match event_result {
Ok(event) => {
// Process the received event
println!("Received event: {:?}", event);
}
Err(err) => {
// Handle the error
eprintln!("Error receiving event: {:?}", err);
}
}
}
Ok(())
}
When you interact with the Azure Event Hubs client library using the Rust SDK, errors returned by the service are returned as azure_core::Error
values using ErrorKind::Other
which are azure_messaging_eventhubs::Error
values.
The Event Hubs SDK client uses the tracing package to enable diagnostics.
See the CONTRIBUTING.md for details on building, testing, and contributing to these libraries.
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://opensource.microsoft.com/cla/.
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.
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.
Azure SDK for Rust is licensed under the MIT license.