Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python将JSonfile发送到EventHub_Python_Json_Python 3.x_Azure Eventhub_Sendfile - Fatal编程技术网

使用Python将JSonfile发送到EventHub

使用Python将JSonfile发送到EventHub,python,json,python-3.x,azure-eventhub,sendfile,Python,Json,Python 3.x,Azure Eventhub,Sendfile,我需要使用Python将JSonfile从我的文件夹发送到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&

我需要使用Python将JSonfile从我的文件夹发送到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/"))
导入json
从azure.eventhub导入EventHubClient、发件人、EventData
#地址可以采用以下格式之一:
#“amqps://:@.servicebus.windows.net/myeventhub”
# "amqps://.servicebus.windows.net/myeventhub"
#如果SAS策略和密钥编码在URL中,则它们不是必需的
地址=”amqps://xxxxxxxxxxxx.servicebus.windows.net/import"
#如果SAS策略和密钥编码在URL中,则它们不是必需的
USER=“xxx”
KEY=“XXXXXXXXXXXXX”
#创建事件中心客户端
client=EventHubClient(地址,调试=False,用户名=USER,密码=KEY)
#将发件人添加到客户端
发送者=客户端。添加发送者(partition=“0”)
#运行事件中心客户端
client.run()
#从下面的文件夹将jsonfile逐个发送到事件中心
sender.send(EventData(“C:/Users/shef123/Desktop/”)

我的代码不起作用,因为我刚刚开始学习python。任何人都可以帮我解决这个问题。

问题在于您尝试将数据发送到事件中心的方式。您不能像下面那样直接调用此函数并传递文件夹以处理文件:

sender.send(EventData("C:/Users/shef123/Desktop/"))
您可以检查此链接以读取json文件:

您可以从文件夹中读取JSON文件,然后使用类似的代码逐个发送

您可以在beloe repo中找到与发送数据相关的代码:

请查看以下链接以供参考:


这里重要的是
EventData
对象只接受字符串或字节。 pythonsdk不会为您读取该文件,只会将该文件路径作为原始数据字符串,并将该字符串发送到事件中心

因此,您需要做的是打开文件并将内容加载到字节数组中,然后将字节传递给
EventData
constructor--
EventData(body=loaded\u bytes)
。 请注意,活动大小有一个配额限制,具体取决于活动时间


值得注意的是,azure eventhub v5已于2020年1月发布

可在pypi上获得:

请按照以下步骤迁移您的程序


还有一个示例文件夹供您开始使用。

不起作用,您能解释一下吗?您是否收到任何错误?Jsonfile已成功发送到EventHub。但问题是当我在应用程序中验证它的错误时并没有成功。错误消息为“使用JsonReader.setLenient(true)在第1行第1列路径$处接受格式错误的JSON”。但是,当我尝试使用Java发送相同的json文件时,应用程序中没有错误,因此json文件或格式不会出现问题。我的问题是使用Java我可以成功地发送json文件,为什么我不能通过Python发送。我遗漏了什么吗?当您指的是sendJSonfile时,您指的是在send方法中发送一个文件或json字符串?还有,您看到的错误消息是什么应用程序?它不只是将目录名作为事件发送吗?