Python / Azure 事件中心

问题描述 投票:0回答:1

我正在编写一个非常基本的 Python 脚本,该脚本假设将示例 JSON 数据发送到 Azure 事件中心。 这是我的脚本:

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开发人员,但我只能看到2个参数被传递给“from_connection_string()”

我可以改变什么来解决这个问题?

python azure-eventhub
1个回答
0
投票

该错误是由于

EventHubProducerClient.from_connection_string
方法的错误使用造成的。
from_connection_string
方法只需要一个参数,即连接字符串。

  • 我尝试了来自 MSDOC 的以下代码。

import asyncio
from azure.eventhub import EventData
from azure.eventhub.aio import EventHubProducerClient
import json
# Azure Event Hubs configuration
CONNECTION_STRING = "   "
EVENT_HUB_NAME = " "

# Create a sample JSON message
json_message = {
    "id": 1234567890,
    "name": "Peter",
    "message": "This is a JSON message."
}

async def send_json_message():
    # Create a producer client to send messages to the event hub using the connection string.
    producer = EventHubProducerClient.from_connection_string(CONNECTION_STRING, eventhub_name=EVENT_HUB_NAME)
    
    try:
        async with producer:
            # Create a batch.
            event_data_batch = await producer.create_batch()

            # Add the JSON message to the batch.
            event_data_batch.add(EventData(body=json.dumps(json_message)))

            # Send the batch of events to the event hub.
            await producer.send_batch(event_data_batch)

        # Print a success message and display the sent JSON message.
        print("Message sent successfully:")
        print(json.dumps(json_message, indent=4))

    except Exception as e:
        # Print an error message and the exception when sending fails.
        print("Failed to send message:")
        print(str(e))

if __name__ == "__main__":
    asyncio.run(send_json_message())

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.