azure-eventhub-py

Plataforma de streaming de big data para ingestão de eventos de alta taxa de transferência.

npx skills add https://github.com/microsoft/agent-skills --skill azure-eventhub-py

Azure Event Hubs SDK for Python

Big data streaming platform for high-throughput event ingestion.

Installation

pip install azure-eventhub azure-identity
# For checkpointing with blob storage
pip install azure-eventhub-checkpointstoreblob-aio

Environment Variables

EVENT_HUB_FULLY_QUALIFIED_NAMESPACE=<namespace>.servicebus.windows.net  # Required for all auth methods
EVENT_HUB_NAME=my-eventhub  # Required for all auth methods
STORAGE_ACCOUNT_URL=https://<account>.blob.core.windows.net  # Required for checkpoint storage
CHECKPOINT_CONTAINER=checkpoints  # Required for checkpoint storage
AZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production

Authentication & Lifecycle

🔑 Two rules apply to every code sample below:

  1. Prefer DefaultAzureCredential. It works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change. Avoid connection strings, account/API keys — they bypass Entra audit and rotation.
    • Local dev: DefaultAzureCredential works as-is.
    • Production: set AZURE_TOKEN_CREDENTIALS=prod (or AZURE_TOKEN_CREDENTIALS=<specific_credential>) to constrain the credential chain to production-safe credentials.
  2. Wrap every client in a context manager so HTTP transports, sockets, and token caches are released deterministically:
    • Sync: with <Client>(...) as client:
    • Async: async with <Client>(...) as client: and async with DefaultAzureCredential() as credential: (from azure.identity.aio)

Snippets may abbreviate this setup, but production code should always follow both rules.

from azure.identity import DefaultAzureCredential, ManagedIdentityCredential
from azure.eventhub import EventHubProducerClient, EventHubConsumerClient

# Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=<specific_credential>
credential = DefaultAzureCredential(require_envvar=True)
# Or use a specific credential directly in production:
# See https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#credential-classes
# credential = ManagedIdentityCredential()
namespace = "<namespace>.servicebus.windows.net"
eventhub_name = "my-eventhub"

# Producer
with EventHubProducerClient(
    fully_qualified_namespace=namespace,
    eventhub_name=eventhub_name,
    credential=credential
) as producer:
    # Use producer here (see following sections for operations)
    ...

# Consumer
with EventHubConsumerClient(
    fully_qualified_namespace=namespace,
    eventhub_name=eventhub_name,
    consumer_group="$Default",
    credential=credential
) as consumer:
    # Use consumer here (see following sections for operations)
    ...

Client Types

ClientPurpose
EventHubProducerClientSend events to Event Hub
EventHubConsumerClientReceive events from Event Hub
BlobCheckpointStoreTrack consumer progress

Send Events

from azure.eventhub import EventHubProducerClient, EventData
from azure.identity import DefaultAzureCredential

with EventHubProducerClient(
    fully_qualified_namespace="<namespace>.servicebus.windows.net",
    eventhub_name="my-eventhub",
    credential=DefaultAzureCredential()
) as producer:
    # Create batch (handles size limits)
    event_data_batch = producer.create_batch()
    
    for i in range(10):
        try:
            event_data_batch.add(EventData(f"Event {i}"))
        except ValueError:
            # Batch is full, send and create new one
            producer.send_batch(event_data_batch)
            event_data_batch = producer.create_batch()
            event_data_batch.add(EventData(f"Event {i}"))
    
    # Send remaining
    producer.send_batch(event_data_batch)

Send to Specific Partition

# By partition ID
event_data_batch = producer.create_batch(partition_id="0")

# By partition key (consistent hashing)
event_data_batch = producer.create_batch(partition_key="user-123")

Receive Events

Simple Receive

from azure.eventhub import EventHubConsumerClient

def on_event(partition_context, event):
    print(f"Partition: {partition_context.partition_id}")
    print(f"Data: {event.body_as_str()}")
    partition_context.update_checkpoint(event)

with EventHubConsumerClient(
    fully_qualified_namespace="<namespace>.servicebus.windows.net",
    eventhub_name="my-eventhub",
    consumer_group="$Default",
    credential=DefaultAzureCredential()
) as consumer:
    consumer.receive(
        on_event=on_event,
        starting_position="-1",  # Beginning of stream
    )

With Blob Checkpoint Store (Production)

from azure.eventhub import EventHubConsumerClient
from azure.eventhub.extensions.checkpointstoreblob import BlobCheckpointStore
from azure.identity import DefaultAzureCredential

checkpoint_store = BlobCheckpointStore(
    blob_account_url="https://<account>.blob.core.windows.net",
    container_name="checkpoints",
    credential=DefaultAzureCredential()
)

with EventHubConsumerClient(
    fully_qualified_namespace="<namespace>.servicebus.windows.net",
    eventhub_name="my-eventhub",
    consumer_group="$Default",
    credential=DefaultAzureCredential(),
    checkpoint_store=checkpoint_store
) as consumer:
    def on_event(partition_context, event):
        print(f"Received: {event.body_as_str()}")
        # Checkpoint after processing
        partition_context.update_checkpoint(event)

    consumer.receive(on_event=on_event)

Async Client

from azure.eventhub.aio import EventHubProducerClient, EventHubConsumerClient
from azure.identity.aio import DefaultAzureCredential
import asyncio

async def send_events():
    credential = DefaultAzureCredential()
    
    async with EventHubProducerClient(
        fully_qualified_namespace="<namespace>.servicebus.windows.net",
        eventhub_name="my-eventhub",
        credential=credential
    ) as producer:
        batch = await producer.create_batch()
        batch.add(EventData("Async event"))
        await producer.send_batch(batch)

async def receive_events():
    async def on_event(partition_context, event):
        print(event.body_as_str())
        await partition_context.update_checkpoint(event)
    
    async with EventHubConsumerClient(
        fully_qualified_namespace="<namespace>.servicebus.windows.net",
        eventhub_name="my-eventhub",
        consumer_group="$Default",
        credential=DefaultAzureCredential()
    ) as consumer:
        await consumer.receive(on_event=on_event)

asyncio.run(send_events())

Event Properties

event = EventData("My event body")

# Set properties
event.properties = {"custom_property": "value"}
event.content_type = "application/json"

# Read properties (on receive)
print(event.body_as_str())
print(event.sequence_number)
print(event.offset)
print(event.enqueued_time)
print(event.partition_key)

Get Event Hub Info

with producer:
    info = producer.get_eventhub_properties()
    print(f"Name: {info['name']}")
    print(f"Partitions: {info['partition_ids']}")
    
    for partition_id in info['partition_ids']:
        partition_info = producer.get_partition_properties(partition_id)
        print(f"Partition {partition_id}: {partition_info['last_enqueued_sequence_number']}")

Best Practices

  1. Pick sync OR async and stay consistent. Do not mix azure.xxx sync clients with azure.xxx.aio async clients in the same call path. Choose one mode per module.
  2. Always use context managers for clients and async credentials. Wrap every client in with Client(...) as client: (sync) or async with Client(...) as client: (async) for proper cleanup. For async DefaultAzureCredential from azure.identity.aio, also use async with credential: so tokens and transports are cleaned up.
  3. Use DefaultAzureCredential for portable auth across local dev and Azure (avoid connection strings / API keys when possible).
  4. Use batches for sending multiple events
  5. Use checkpoint store in production for reliable processing
  6. Use async client for high-throughput scenarios
  7. Use partition keys for ordered delivery within a partition
  8. Handle batch size limits — catch ValueError when batch is full
  9. Set appropriate consumer groups for different applications

Reference Files

FileContents
references/checkpointing.mdCheckpoint store patterns, blob checkpointing, checkpoint strategies
references/partitions.mdPartition management, load balancing, starting positions
scripts/setup_consumer.pyCLI for Event Hub info, consumer setup, and event sending/receiving

Mais skills de microsoft

oss-growth
microsoft
Persona de growth hacker OSS
official
microsoft-foundry
microsoft
Implantar, avaliar e gerenciar agentes Foundry de ponta a ponta: build Docker, push ACR, criação de agente hospedado/prompt, inicialização de contêiner, avaliação em lote, avaliação contínua, fluxos de trabalho do otimizador de prompt, agent.yaml, curadoria de conjunto de dados a partir de rastros. USE PARA: implantar agente no Foundry, agente hospedado, criar agente, invocar agente, avaliar agente, executar avaliação em lote, avaliação contínua, monitoramento contínuo, status da avaliação contínua, otimizar prompt, melhorar prompt, otimizador de prompt, otimizar instruções do agente, melhorar agente...
officialdevelopmentdevops
azure-ai
microsoft
Use para Azure AI: Search, Speech, OpenAI, Document Intelligence. Ajuda com pesquisa, busca vetorial/híbrida, fala para texto, texto para fala, transcrição, OCR. QUANDO: AI Search, pesquisa de consulta, busca vetorial, busca híbrida, busca semântica, fala para texto, texto para fala, transcrever, OCR, converter texto em fala.
officialdevelopmentapi
azure-deploy
microsoft
Execute implantações do Azure para aplicativos JÁ PREPARADOS que possuem arquivos .azure/deployment-plan.md e de infraestrutura existentes. NÃO use esta skill quando o usuário pedir para CRIAR um novo aplicativo — use azure-prepare. Esta skill executa comandos azd up, azd deploy, terraform apply e az deployment com recuperação de erros integrada. Requer .azure/deployment-plan.md do azure-prepare e status validado do azure-validate. QUANDO: "executar azd up", "executar azd deploy", "executar implantação",...
officialdevopsaws
azure-storage
microsoft
Serviços de Armazenamento do Azure, incluindo Blob Storage, File Shares, Queue Storage, Table Storage e Data Lake. Responde a perguntas sobre camadas de acesso ao armazenamento (hot, cool, cold, archive), quando usar cada camada e comparação entre elas. Oferece armazenamento de objetos, compartilhamentos de arquivos SMB, mensagens assíncronas, NoSQL chave-valor e análise de big data. Inclui gerenciamento de ciclo de vida. USE PARA: blob storage, file shares, queue storage, table storage, data lake, upload de arquivos, download de blobs, contas de armazenamento, camadas de acesso,...
officialdevelopmentdatabase
azure-diagnostics
microsoft
Depure problemas de produção no Azure usando AppLens, Azure Monitor, integridade de recursos e triagem segura. QUANDO: depurar problemas de produção, solucionar problemas do Serviço de Aplicativo, alto uso de CPU no Serviço de Aplicativo, falha de implantação do Serviço de Aplicativo, solucionar problemas de aplicativos em contêineres, solucionar problemas de funções, solucionar problemas do AKS, kubectl não consegue conectar, falhas do kube-system/CoreDNS, pod pendente, crashloop, nó não pronto, falhas de atualização, analisar logs, KQL, insights, falhas ao puxar imagem, problemas de inicialização a frio, falhas de sonda de integridade,...
officialdevopsdevelopment
azure-prepare
microsoft
Prepare aplicativos do Azure para implantação (infra Bicep/Terraform, azure.yaml, Dockerfiles). Use para criar/modernizar ou criar+implantar; não para migração entre nuvens (use azure-cloud-migrate). NÃO USE PARA: aplicativos copilot-sdk (use azure-hosted-copilot-sdk). QUANDO: "criar aplicativo", "construir aplicativo web", "criar API", "criar API HTTP serverless", "criar frontend", "criar backend", "construir um serviço", "modernizar aplicativo", "atualizar aplicativo", "adicionar autenticação", "adicionar cache", "hospedar no Azure", "criar e...
officialdevelopmentdevops
azure-validate
microsoft
Validação pré-implantação para prontidão do Azure. Execute verificações aprofundadas de configuração, infraestrutura (Bicep ou Terraform), atribuições de função RBAC, permissões de identidade gerenciada e pré-requisitos antes de implantar. QUANDO: validar meu aplicativo, verificar prontidão para implantação, executar verificações de pré-voo, verificar configuração, verificar se está pronto para implantar, validar azure.yaml, validar Bicep, testar antes de implantar, solucionar erros de implantação, validar Azure Functions, validar function app, validar serverless...
officialdevopstesting