事件中心客户端_azure-eventhub-rust

事件中心客户端_azure-eventhub-rust

以下为本文档的中文说明

azure-eventhub-rust 是 Azure Event Hubs 在 Rust 语言上的客户端 SDK 技能。Azure Event Hubs 是一个大数据流式处理平台和事件引入服务,能够每秒处理数百万条事件,适用于实时数据分析、物联网设备数据采集、日志流处理等场景。该技能的核心概念包括:命名空间(Namespace)——Event Hubs 的容器;事件中心(Event Hub)——分区后用于并行处理的事件流;分区(Partition)——有序的事件序列;生产者(Producer)——向事件中心发送事件的客户端;消费者(Consumer)——从分区接收事件的客户端。使用场景包括:在 Rust 应用中发送和接收事件、实现流式数据引入、构建实时数据处理管道。安装方式简单,只需在 Cargo.toml 中添加 azure_messaging_eventhubs 和 azure_identity 两个依赖包。配置方面需要设置 EVENTHUBS_HOST 和 EVENTHUB_NAME 两个环境变量。该技能提供了 ProducerClient 和 ConsumerClient 的完整 API 示例代码,包括使用 DeveloperToolsCredential 进行身份验证、创建生产者客户端、发送事件到事件中心、以及从分区消费事件等操作。它适合需要在 Rust 生态系统中使用 Azure 云服务的开发者,特别是从事物联网、日志处理和实时分析项目的团队。


Azure Event Hubs SDK for Rust

Client library for Azure Event Hubs — big data streaming platform and event ingestion service.

Installation

cargoaddazure_messaging_eventhubs azure_identity

Environment Variables

EVENTHUBS_HOST=<namespace>.servicebus.windows.netEVENTHUB_NAME=<eventhub-name>

Key Concepts

  • Namespace— container for Event Hubs
  • Event Hub— stream of events partitioned for parallel processing
  • Partition— ordered sequence of events
  • Producer— sends events to Event Hub
  • Consumer— receives events from partitions

Producer Client

Create Producer

useazure_identity::DeveloperToolsCredential;useazure_messaging_eventhubs::ProducerClient;letcredential=DeveloperToolsCredential::new(None)?;letproducer=ProducerClient::builder().open("<namespace>.servicebus.windows.net","eventhub-name",credential.clone()).await?;

Send Single Event

producer.send_event(vec![1,2,3,4],None).await?;

Send Batch

letbatch=producer.create_batch(None).await?;batch.try_add_event_data(b"event 1".to_vec(),None)?;batch.try_add_event_data(b"event 2".to_vec(),None)?;producer.send_batch(batch,None).await?;

Consumer Client

Create Consumer

useazure_messaging_eventhubs::ConsumerClient;letcredential=DeveloperToolsCredential::new(None)?;letconsumer=ConsumerClient::builder().open("<namespace>.servicebus.windows.net","eventhub-name",credential.clone()).await?;

Receive Events

// Open receiver for specific partitionletreceiver=consumer.open_partition_receiver("0",None).await?;// Receive eventsletevents=receiver.receive_events(100,None).await?;foreventinevents{println!("Event data: {:?}",event.body());}

Get Event Hub Properties

letproperties=consumer.get_eventhub_properties(None).await?;println!("Partitions: {:?}",properties.partition_ids);

Get Partition Properties

letpartition_props=consumer.get_partition_properties("0",None).await?;println!("Last sequence number: {}",partition_props.last_enqueued_sequence_number);

Best Practices

  1. Reuse clients— create once, send many events
  2. Use batches— more efficient than individual sends
  3. Check batch capacitytry_add_event_datareturns false when full
  4. Process partitions in parallel— each partition can be consumed independently
  5. Use consumer groups— isolate different consuming applications
  6. Handle checkpointing— useazure_messaging_eventhubs_checkpointstore_blobfor distributed consumers

Checkpoint Store (Optional)

For distributed consumers with checkpointing:

cargoaddazure_messaging_eventhubs_checkpointstore_blob

Reference Links

ResourceLink
API Referencehttps://docs.rs/azure_messaging_eventhubs
Source Codehttps://github.com/Azure/azure-sdk-for-rust/tree/main/sdk/eventhubs/azure_messaging_eventhubs
crates.iohttps://crates.io/crates/azure_messaging_eventhubs

When to Use

This skill is applicable to execute the workflow or actions described in the overview.

Limitations

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.