如何使用 Python 作为实时数据流将 CSV 文件逐条记录发送到 Azure 事件中心
How to send a CSV file record by record to an Azure Event hub using Python as a real time data stream
我尝试使用 Microsoft 文档 (https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-python-get-started-send) 中提供的示例代码将消息发送到事件中心。我可以使用相同的代码将 CSV 文件记录发送到事件中心吗
更新0904:如果您要批量发送EventData,请按照以下代码:
ADDRESS = "amqps://xxx.servicebus.windows.net/xxx"
# SAS policy and key are not required if they are encoded in the URL
USER = "RootManageSharedAccessKey"
KEY = "xxx"
#this generator method is just an example, please implement your own logic in the data_generator
def data_generator():
for i in range(5):
yield b"hello world!!! hhhhhhhhheee"
if not ADDRESS:
raise ValueError("No EventHubs URL supplied.")
# Create Event Hubs client
client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
sender = client.add_sender(partition="0")
client.run()
#send the batch of eventData
data = EventData(batch=data_generator())
sender.send(data)
如果您的意思是在 .csv 文件的每个单元格中发送每条消息,you can write your own logic to fetch each message in .csv file
(请尝试 google 实现),然后使用 for
循环将它们发送到事件中心。
只需对 official doc 中的代码进行一些更改:
logger = logging.getLogger("azure")
ADDRESS = "amqps://xx.servicebus.windows.net/xxx"
# SAS policy and key are not required if they are encoded in the URL
USER = "RootManageSharedAccessKey"
KEY = "xxxx"
if not ADDRESS:
raise ValueError("No EventHubs URL supplied.")
# Create Event Hubs client
client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
#if you ommit the parameter partition="0", the events will be distributed to all available partitions via round-robin
sender = client.add_sender(partition="0")
client.run()
#you should write your own logic to fetch each message in .csv file
#you then can use for loop to send each message in .csv like below, the following is just an test example
for i in range(10):
print("Sending message: {}".format(i))
sender.send(EventData("this is msg: "+str(i)))
我尝试使用 Microsoft 文档 (https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-python-get-started-send) 中提供的示例代码将消息发送到事件中心。我可以使用相同的代码将 CSV 文件记录发送到事件中心吗
更新0904:如果您要批量发送EventData,请按照以下代码:
ADDRESS = "amqps://xxx.servicebus.windows.net/xxx"
# SAS policy and key are not required if they are encoded in the URL
USER = "RootManageSharedAccessKey"
KEY = "xxx"
#this generator method is just an example, please implement your own logic in the data_generator
def data_generator():
for i in range(5):
yield b"hello world!!! hhhhhhhhheee"
if not ADDRESS:
raise ValueError("No EventHubs URL supplied.")
# Create Event Hubs client
client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
sender = client.add_sender(partition="0")
client.run()
#send the batch of eventData
data = EventData(batch=data_generator())
sender.send(data)
如果您的意思是在 .csv 文件的每个单元格中发送每条消息,you can write your own logic to fetch each message in .csv file
(请尝试 google 实现),然后使用 for
循环将它们发送到事件中心。
只需对 official doc 中的代码进行一些更改:
logger = logging.getLogger("azure")
ADDRESS = "amqps://xx.servicebus.windows.net/xxx"
# SAS policy and key are not required if they are encoded in the URL
USER = "RootManageSharedAccessKey"
KEY = "xxxx"
if not ADDRESS:
raise ValueError("No EventHubs URL supplied.")
# Create Event Hubs client
client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
#if you ommit the parameter partition="0", the events will be distributed to all available partitions via round-robin
sender = client.add_sender(partition="0")
client.run()
#you should write your own logic to fetch each message in .csv file
#you then can use for loop to send each message in .csv like below, the following is just an test example
for i in range(10):
print("Sending message: {}".format(i))
sender.send(EventData("this is msg: "+str(i)))