【发布时间】:2020-01-13 11:15:43
【问题描述】:
我需要使用 Python 将 jsonfiles 从我的文件夹发送到 azure-EventHub
import json
from azure.eventhub import EventHubClient, Sender, EventData
# Address can be in either of these formats:
# "amqps://<URL-encoded-SAS-policy>:<URL-encoded-SAS-key>@<mynamespace>.servicebus.windows.net/myeventhub"
# "amqps://<mynamespace>.servicebus.windows.net/myeventhub"
# SAS policy and key are not required if they are encoded in the URL
ADDRESS = "amqps://xxxxxxxxxxxx.servicebus.windows.net/import"
# SAS policy and key are not required if they are encoded in the URL
USER = "xxx"
KEY = "xxxxxxxxxxxxxx"
# Create an Event Hubs client
client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
# Add a sender to the client
sender = client.add_sender(partition="0")
# Run the Event Hub client
client.run()
# Send jsonfile one by one to the event hub from below folder
sender.send(EventData("C:/Users/shef123/Desktop/"))
我的代码不起作用,因为我刚开始学习 python。谁能帮我解决这个问题。
【问题讨论】:
-
没用,能解释一下吗?你有什么错误吗?
-
Jsonfile 成功发送到 EventHub。但问题是当我在应用程序中验证它的错误不成功时。错误消息为“使用 JsonReader.setLenient(true) 在第 1 列第 1 路径 $ 处接受格式错误的 JSON”。但是,当我尝试使用 Java 发送相同的 json 文件时,应用程序中没有错误,因此它不是 JSON 文件或格式的问题。我的问题是使用 Java 我可以成功发送 json 文件,为什么我不能通过 Python 发送。我错过了什么吗?
-
当你的意思是发送jsonfile时,你的意思是在send方法中发送一个文件或json字符串?以及您看到错误消息的应用程序是什么?
标签: python json python-3.x azure-eventhub sendfile