azure-eventhub-rust
von microsoft
Client-Bibliothek für Azure Event Hubs – Big-Data-Streaming-Plattform und Ereigniserfassungsdienst.
npx skills add https://github.com/microsoft/skills --skill azure-eventhub-rustAzure Event Hubs library for Rust
Client library for Azure Event Hubs — send and receive events for streaming data ingestion.
Use this skill when:
- An app needs to send events to Azure Event Hubs from Rust
- You need to receive and process events from partitions
- You need batch sending for throughput optimization
- You need to control consumer start position
IMPORTANT: Only use the official
azure_messaging_eventhubscrate published by the azure-sdk crates.io user. Do NOT use unofficial or community crates. Official crates use underscores in names and none have version 0.21.0.
Installation
cargo add azure_messaging_eventhubs azure_identity tokio futures
If your code uses
azure_coretypes directly, addazure_coretoCargo.toml. If you only useazure_messaging_eventhubsre-exports, directazure_coredependency is optional.
Environment Variables
EVENTHUBS_HOST=<namespace>.servicebus.windows.net # Required — fully qualified namespace
EVENTHUB_NAME=<eventhub-name> # Required — name of the Event Hub
Key Concepts
| Concept | Description |
|---|---|
| Namespace | Container for one or more Event Hubs |
| Event Hub | Stream of events, partitioned for parallel reads |
| Partition | Ordered, append-only sequence of events |
| Producer | Sends events via ProducerClient |
| Consumer | Receives events from partitions via ConsumerClient |
Authentication
use azure_identity::DeveloperToolsCredential;
use azure_messaging_eventhubs::ProducerClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Local dev: DeveloperToolsCredential. Production: use ManagedIdentityCredential.
let credential = DeveloperToolsCredential::new(None)?;
let producer = ProducerClient::builder()
.open("<namespace>.servicebus.windows.net", "<eventhub-name>", credential.clone())
.await?;
Ok(())
}
Core Workflow
Send Events
// Send a single event
producer.send_event(vec![1, 2, 3, 4], None).await?;
Send Batch
let batch = producer.create_batch(None).await?;
batch.try_add_event_data(vec![1, 2, 3, 4], None)?;
producer.send_batch(batch, None).await?;
Receive Events
use azure_identity::DeveloperToolsCredential;
use azure_messaging_eventhubs::ConsumerClient;
// Local dev: DeveloperToolsCredential. Production: use ManagedIdentityCredential.
let credential = DeveloperToolsCredential::new(None)?;
let consumer = ConsumerClient::builder()
.open("<namespace>.servicebus.windows.net", "<eventhub-name>", credential.clone())
.await?;
Receive from Partition
use futures::stream::StreamExt;
use azure_messaging_eventhubs::{
ConsumerClient, OpenReceiverOptions, StartLocation, StartPosition,
};
let receiver = consumer
.open_receiver_on_partition(
"0".to_string(),
Some(OpenReceiverOptions {
start_position: Some(StartPosition {
location: StartLocation::Earliest,
..Default::default()
}),
..Default::default()
}),
)
.await?;
let mut stream = receiver.stream_events();
while let Some(event_result) = stream.next().await {
match event_result {
Ok(event) => println!("Received: {:?}", event),
Err(err) => eprintln!("Error: {:?}", err),
}
}
RBAC Roles
For Entra ID auth, assign one of these roles:
| Role | Access |
|---|---|
Azure Event Hubs Data Sender | Send events |
Azure Event Hubs Data Receiver | Receive events |
Azure Event Hubs Data Owner | Full access |
Best Practices
- Use
cargo addto manage dependencies, never editCargo.tomldirectly. Add and remove Rust SDK dependencies with cargo commands instead of manual manifest edits. - Add
azure_coreonly when importingazure_coretypes directly. If your code importsazure_core::http::Url,azure_core::http::RequestContent, orazure_core::error::ErrorKind, includeazure_core; otherwise a direct dependency is optional. - Use
DeveloperToolsCredentialfor local dev,ManagedIdentityCredentialfor production — Rust does not provide a singleDefaultAzureCredentialtype - Never hardcode credentials — use environment variables or managed identity
- Use batching —
create_batch+send_batchfor throughput optimization - Handle errors per event — match on
Ok/Errin the event stream - Specify start position — use
StartLocation::EarliestorStartLocation::Latestto control where consumption begins