亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 JSON 文件發送到 Azure 事件中心

將 JSON 文件發送到 Azure 事件中心

汪汪一只貓 2023-07-27 10:28:25
我希望使用 Microsoft 網站中給出的示例代碼將 JSON 文件發送到 Azure 事件中心。該代碼看起來不正確,并且在事件中心沒有看到以下上傳的文件。有人可以幫忙解決如何發送實際的 JSON 文件嗎?import asynciofrom azure.eventhub import EventDatafrom azure.eventhub.aio import EventHubProducerClientasync def run():    producer = EventHubProducerClient.from_connection_string(        conn_str="foo",        eventhub_name="boo")    async with producer:        event_data_batch = await producer.create_batch()        event_data_batch.add(EventData(JSONFilepath))        await producer.send_batch(event_data_batch)loop = asyncio.get_event_loop()loop.run_until_complete(run())注意:我在運行程序時沒有遇到錯誤。
查看完整描述

1 回答

?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

將 JSON 對象和 JSON 字符串發送到事件中心的代碼片段


import asyncio

import nest_asyncio

nest_asyncio.apply()

from azure.eventhub.aio import EventHubProducerClient

from azure.eventhub import EventData

import json


async def run():

    # Create a producer client to send messages to the event hub.

    # Specify a connection string to your event hubs namespace and

        # the event hub name.

    producer = EventHubProducerClient.from_connection_string("<>", eventhub_name="<>")

    async with producer:

        # Create a batch.

        event_data_batch = await producer.create_batch()


        # Add events to the batch.


        #Method 1 - You provide a JSON string 

        body1 = '{"id":"device2","timestamp":"2016-01-17T01:17:00Z"}' 

        event_data_batch.add(EventData(body1))


        #Method 2 - You get the JSON Object and convert to string

        json_obj = {"id":"device3","timestamp":"2016-01-18T01:17:00Z"}

        body2= json.dumps(json_obj)

        event_data_batch.add(EventData(body2))



        #This just sending the string which will not be captured by TSI

        event_data_batch.add(EventData('Third event'))


        # Send the batch of events to the event hub.

        await producer.send_batch(event_data_batch)



loop = asyncio.get_event_loop()

loop.run_until_complete(run())


查看完整回答
反對 回復 2023-07-27
  • 1 回答
  • 0 關注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號