You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python脚本向Azure Event Hubs发JSON数据报错求助

向Azure Event Hubs发送JSON数据的Python脚本报错解决

问题描述

我正在编写一个基础Python脚本,用于向Azure Event Hubs发送示例JSON数据,脚本代码如下:

import asyncio
from azure.eventhub.aio import EventHubProducerClient

async def send_json_message(connection_string, event_hub_name, json_message):
  """Sends a JSON message to an Azure Event Hub.

  Args:
    connection_string: The connection string to your Azure Event Hubs namespace.
    event_hub_name: The name of your event hub.
    json_message: The JSON message to send.
  """

  producer = EventHubProducerClient.from_connection_string(connection_string, event_hub_name)

  async with producer:
    event_data_batch = await producer.create_batch()
    event_data_batch.add(EventData(body=json.dumps(json_message)))
    await producer.send_batch(event_data_batch)

if __name__ == "__main__":
  connection_string = "Endpoint=sb://auditblobevents.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=k9GhYMwABTBa6PuHKczIolE7FJeR0bOpQ+AEhEgMAY8="
  event_hub_name = "auditblobtopic"
  json_message = {
    "id": 1234567890,
    "name": "Peter",
    "message": "This is a JSON message."
  }

  asyncio.run(send_json_message(connection_string, event_hub_name, json_message))

执行命令python3 send.py运行脚本时,出现以下错误:

Traceback (most recent call last):
  File "send.py", line 29, in <module>
    asyncio.run(send_json_message(connection_string, event_hub_name, json_message))
  File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "send.py", line 13, in send_json_message
    producer = EventHubProducerClient.from_connection_string(connection_string, event_hub_name)
TypeError: from_connection_string() takes 2 positional arguments but 3 were given

我并非Python开发者,但我认为仅向from_connection_string()传入了2个参数,请问需要如何修改才能解决该问题?

解决方法

1. 修正from_connection_string传参方式

报错原因是Azure Event Hubs Python SDK要求event_hub_name必须以关键字参数形式传入,不能用位置传参。修改这一行代码:

producer = EventHubProducerClient.from_connection_string(connection_string, event_hub_name=event_hub_name)

2. 修复其他潜在问题

脚本还有两个隐藏错误需要修正:

  • 缺少EventData类的导入,需添加:
    from azure.eventhub import EventData
    
  • 缺少json模块的导入,需添加:
    import json
    

最终修正后的完整代码

import asyncio
import json
from azure.eventhub import EventData
from azure.eventhub.aio import EventHubProducerClient

async def send_json_message(connection_string, event_hub_name, json_message):
  """Sends a JSON message to an Azure Event Hub.

  Args:
    connection_string: The connection string to your Azure Event Hubs namespace.
    event_hub_name: The name of your event hub.
    json_message: The JSON message to send.
  """

  producer = EventHubProducerClient.from_connection_string(connection_string, event_hub_name=event_hub_name)

  async with producer:
    event_data_batch = await producer.create_batch()
    event_data_batch.add(EventData(body=json.dumps(json_message)))
    await producer.send_batch(event_data_batch)

if __name__ == "__main__":
  connection_string = "Endpoint=sb://auditblobevents.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=k9GhYMwABTBa6PuHKczIolE7FJeR0bOpQ+AEhEgMAY8="
  event_hub_name = "auditblobtopic"
  json_message = {
    "id": 1234567890,
    "name": "Peter",
    "message": "This is a JSON message."
  }

  asyncio.run(send_json_message(connection_string, event_hub_name, json_message))

验证运行

重新执行python3 send.py,无报错则说明消息已成功发送至Azure Event Hubs。


内容的提问来源于stack exchange,提问作者Eugene Goldberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 17:43:16